/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
* 
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 * USBDevice.c
 *
 * Project:
 * --------
 *   MMI
 *
 * Description:
 * ------------
 *   This file is for mmi configure usb device
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/

    #include "MMI_features.h"

#ifdef __MMI_USB_SUPPORT__
#ifdef __MMI_USB_SPLIT_SUPPORT__


#include "MMIDataType.h"
#include "kal_non_specific_general_types.h"
#include "mmi_frm_events_gprot.h"
#include "FileMgrSrvGProt.h"
#include "MMI_common_app_trc.h"
#include "DebugInitDef_Int.h"
#include "kal_trace.h"
#include "mmi_common_app_trc.h"
#include "NotificationGprot.h"
#include "stack_config.h"
#include "mmi_rp_app_charger_def.h"
#include "wgui_categories_util.h"
#include "wgui_categories_popup.h"
#include "GlobalResDef.h"
#include "custom_events_notify.h"
#include "GlobalConstants.h"
#include "mmi_frm_history_gprot.h"
#include "device.h"
#include "uart_sw.h"
#include "EngineerModeResDef.h"
#include "CustDataRes.h"
#include "mmi_rp_app_idle_def.h"
#include "IdleAppResDef.h"

#ifdef __MMI_DSM_NEW__
#include "mrp_include.h"
#endif

#ifdef __MMI_ENGINEER_MODE__
#endif 

#include "ucmgprot.h"
#include "ucmsrvgprot.h"
#include "gpiosrvgprot.h"

#include "menucuigprot.h"
#include "BootupSrvGprot.h"
#include "ShutdownSrvGprot.h"
#include "IdleGprot.h"
#include "AlarmFrameworkProt.h"
#include "NwUsabSrvGprot.h"

#ifdef __DM_LAWMO_SUPPORT__
#include "mmiapi_dm_struct.h"
#endif

/* USB Device Headder Files */
#include "USBDeviceGprot.h"
#include "USBDeviceDefs.h"
#include "USBSrvGprot.h"

#include "Mmi_frm_scenario_gprot.h"
#include "mmi_rp_app_usbmmi_def.h"
#include "mmi_rp_app_restore_def.h"

#define MMI_USB_MENU_CUI

/***************************************************************************** 
* Define
*****************************************************************************/

/***************************************************************************** 
* Typedef
*****************************************************************************/

/***************************************************************************** 
* Extern Variable
*****************************************************************************/

/***************************************************************************** 
* Extern Functions
*****************************************************************************/


/***************************************************************************** 
* Local Functions
*****************************************************************************/
static void mmi_usb_config_first_page(U8 mode);

static void mmi_usb_entry_progress_screen(void);
static void mmi_usb_close_progress_screen(void);

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display(void);
static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify(mmi_scenario_id scen_id, void *arg);
static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_test(void);
static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify_dummy(mmi_scenario_id scen_id, void *arg);

/* Screen group*/
static void mmi_usb_enter_normal_group(void);
static MMI_ID mmi_usb_get_normal_group(void);
static MMI_ID mmi_usb_get_normal_group_id(void);
static mmi_ret mmi_usb_normal_group_proc(mmi_event_struct *param);
static void mmi_usb_init_detect_query_list(void);
static void mmi_usb_entry_usb_detect_query_scrn_ind(void);
static void mmi_usb_entry_progress_screen_int(void);
static mmi_ret mmi_usb_progress_leave_proc(mmi_event_struct *param);

static void mmi_usb_enter_detect_query_list(void);
static void mmi_usb_query_list_select_hdlr(cui_menu_event_struct *event);
static void mmi_usb_close_detect_query_list(void);
static mmi_ret mmi_usb_detect_cfg_leave_proc(mmi_event_struct *param);
#ifdef __USB_IN_NORMAL_MODE__
static MMI_BOOL mmi_usb_need_to_reboot(void);
#endif

#ifdef __MMI_WEBCAM__
static void mmi_usb_config_webcam(void);
static void mmi_usb_exit_webcam(void);
#endif
static void mmi_usb_display_popup(U16 string, mmi_event_notify_enum type);

/***************************************************************************** 
* Local Variable
*****************************************************************************/
MMI_USB_CONTEXT g_mmi_usb_cntx;
MMI_USB_CONTEXT *mmi_usb_ptr = &g_mmi_usb_cntx;
static S32 highlightedItemIndex = 0;

/* USB split revise*/
/*****************************************************************************
 * FUNCTION
 *  mmi_permit_usb_cfg_screen_display
 * DESCRIPTION
 *  This function check the if usb config screen display allowed or not.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: T/F(?)
 *****************************************************************************/
static MMI_BOOL mmi_permit_usb_cfg_screen_display(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!srv_bootup_is_completed() || 
        //GetActiveScreenId() == SCR_RESTORE_PROCESSING ||    /* During Restore Factory */
       // GetActiveScreenId() == POPUP_SCREENID ||
       // GetActiveScreenId() == SCR_ID_SAT_MAINMENU ||
        srv_fmgr_async_is_busy() ||
        mmi_phb_check_processing()==MMI_TRUE ||
        mmi_secset_is_in_puk_check() ||                 /* PUK needed after SIM lock failed or change PIN1 failed */
#ifdef __MMI_DUAL_SIM_MASTER__
        mmi_secset_is_in_sim2_puk_check() ||
#endif    	
    #if defined(__DM_LAWMO_SUPPORT__)
        ((kal_int8)mmi_dmui_is_phone_lock()!=0) ||
        mmi_dmui_get_status(MMI_DMUI_MO_TYPE_LAWMO_LOCK) == MMI_DMUI_STATUS_LAWMO_LOCK_ON_LOCKING ||
    #endif
        srv_shutdown_is_running()) 
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_SCR_NOT_ALLOW_FALSE);
		//srv_usb_block_event(EVT_ID_USB_PERMIT_CFG);
        return MMI_FALSE;
    }
    else
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_SCR_NOT_ALLOW_TURE);
        return MMI_TRUE;
    }
}

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_test(void)
{
	MMI_BOOL ret;

    ret = mmi_frm_nmgr_notify_by_app(MMI_SCENARIO_ID_DEFAULT, MMI_EVENT_NON_TONE, 
        mmi_usb_permit_usb_cfg_screen_display_notify_dummy, NULL);

    return ret;
}

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify(mmi_scenario_id scen_id, void *arg)
{
    SRV_USB_STATUS usb_state = srv_usb_get_status();

    kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_permit_usb_cfg_screen_display_notify, state=%d", usb_state);

    if (usb_state == SRV_USBSTATUS_PLUGIN)
    {
    	//kal_prompt_trace(MOD_MMI, "[USB MMI] MMI_USBSTATUS_PLUGIN");
    }
    else if (usb_state == SRV_USBSTATUS_PLUGIN && srv_usb_is_connected())
    {
        //kal_prompt_trace(MOD_MMI, "[USB MMI] MMI_USBSTATUS_PLUGOUT && MMIUSB_MASK_IS_CONNECT");
    }
#ifdef __USB_IN_NORMAL_MODE__
    else if (srv_usb_is_leaving_ms_mode())
    {
        //kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_is_leaving_ms_mode");
    }
#endif /* __USB_IN_NORMAL_MODE__ */ 
    else
    {
        kal_prompt_trace(MOD_MMI, "[USB MMI] Not continue show USB cfg screen");
        return MMI_FALSE;
    }

    if ((srv_bootup_get_booting_mode() != SRV_BOOTUP_MODE_NORMAL &&
          !mmi_bootup_is_in_power_on_period()) ||
          mmi_permit_usb_cfg_screen_display() == MMI_TRUE)
    {
        srv_usb_unblock_event(EVT_ID_USB_PERMIT_CFG);
        return MMI_TRUE;
    }
    else
    {
        srv_usb_block_event(EVT_ID_USB_PERMIT_CFG);
        return MMI_FALSE;
    }
    
}

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify_dummy(mmi_scenario_id scen_id, void *arg)
{
    return MMI_TRUE;
}

/*****************************************************************************
 * FUNCTION
 *  UsbDetectIndHdlr
 * DESCRIPTION
 *  This function is usb detect indication event handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
mmi_ret mmi_usb_handle_query_permit_show(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
	MMI_BOOL ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ret = mmi_frm_nmgr_notify_by_app(MMI_SCENARIO_ID_DEFAULT, MMI_EVENT_NON_TONE, 
        mmi_usb_permit_usb_cfg_screen_display_notify, NULL);
/* USB split revise*/
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
    return MMI_RET_OK;    
}

/* USB split revise*/
/*****************************************************************************
 * FUNCTION
 *  mmi_usb_handle_query_permit_pre_check
 * DESCRIPTION
 *  
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
mmi_ret mmi_usb_handle_query_permit_pre_check(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_BOOL ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ret = mmi_permit_usb_cfg_screen_display();
    if (ret)
    {
        srv_usb_unblock_event(EVT_ID_USB_PERMIT_CFG_PRE_CHECK);
    }
    else
    {
        srv_usb_block_event(EVT_ID_USB_PERMIT_CFG_PRE_CHECK);
    }

    return MMI_RET_OK;
}

/*****************************************************************************
 * FUNCTION
 *  UsbDetectIndHdlr
 * DESCRIPTION
 *  This function is usb detect indication event handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
mmi_ret mmi_usb_handle_plug_out(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    SRV_USB_STATUS usb_state = srv_usb_get_status();

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	 switch (usb_state)
    {
        case SRV_USBSTATUS_CONF_PAGE:
        {
            /* Show the remove popup, then go on */
            BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
            /* detect last query screen is in history */
            mmi_usb_close_detect_query_list();
            srv_usb_set_status(SRV_USBSTATUS_PLUGOUT);
            break;
        }
    #ifdef __MMI_PICT_BRIDGE_SUPPORT__
        case SRV_USBSTATUS_PICTBRIGE:
            /* notify pict_brige usb plug-out event */
           // mmi_pict_dps_disconnect_ind();
    #endif /* __MMI_PICT_BRIDGE_SUPPORT__ */
        case SRV_USBSTATUS_CDCACM:
        case SRV_USBSTATUS_CDCACM_PROC:
        case SRV_USBSTATUS_PLUGIN:
        case SRV_USBSTATUS_IGNORED:    
            /* Show the remove popup, then go on */
            if (srv_bootup_is_booting() == MMI_FALSE)
            {
                if (usb_state != SRV_USBSTATUS_PLUGIN)// || PermitUsbCfgScreenDisplay() == MMI_TRUE)
                {
                    BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
                }
            }
        case SRV_USBSTATUS_PLUGOUT:
        {
            mmi_usb_close_detect_query_list();
            srv_usb_set_status(SRV_USBSTATUS_PLUGOUT);
            break;
        }
        case SRV_USBSTATUS_MS_PROC:
        case SRV_USBSTATUS_MS:
        {        
                        break;
        }
    #ifdef __MMI_WEBCAM__
        case SRV_USBSTATUS_WEBCAM:
        {
            mmi_usb_close_detect_query_list();
            BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);                    
            //mmi_usb_webcam_plug_out();
            srv_usb_set_status(SRV_USBSTATUS_PLUGOUT);
            break;
        }
    #endif /* __MMI_WEBCAM__ */ 

    #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
        case SRV_USBSTATUS_JAVA_CONNECT:
        {
            /* Show the remove popup, then go on */
            if (srv_bootup_is_booting() == MMI_FALSE)
            {
                BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
            }

            jvm_comm_usb_in_java_register_cb(MMI_FALSE);

            mmi_usb_close_detect_query_list();
            srv_usb_set_status(SRV_USBSTATUS_PLUGOUT);
            break;
        }					
    #endif				
	
        case SRV_USBSTATUS_UNKNOW:
        default:
        {
            mmi_usb_close_detect_query_list();
            srv_usb_set_status(SRV_USBSTATUS_PLUGOUT);
            break;
        }
    }

	 return MMI_RET_OK;
}

/*****************************************************************************
 * FUNCTION
 *  EntryUsbDetectQueryScr
 * DESCRIPTION
 *  This function is to display "Usb Config:" screen as received USB_DETECT_IND
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: none(?)
 *****************************************************************************/
mmi_ret mmi_usb_handle_plug_in(mmi_event_struct *evt)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/
	
	/*----------------------------------------------------------------*/
	/* Code Body													  */
	/*----------------------------------------------------------------*/
	kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_handle_plug_in");

    srv_backlight_turn_on(1);
    mmi_usb_enter_normal_group();
    mmi_usb_entry_usb_detect_query_scrn_ind();
	return MMI_RET_OK;
}

static void mmi_usb_entry_usb_detect_query_scrn_ind(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /* Watch out: must check PermitUsbCfgScreenDisplay first */

    U8 *guiBuffer = NULL;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_DETECT_QUERY_SCR);

   /*************************************************/
    /* Turn on backlight before execute exit handler */
    /* Turn on backlight will blt out the LCD buffer */
    /* If execute exit handler before turn on BL     */
    /* migh see the blue background because turn on  */
    /* BL only blt out layer 0, in idle screen,      */
    /* layer 0 is not wallpaper, wallpaper locate at */
    /* layre 1.                                      */
   /*************************************************/
    /* USB split revise*/
    //srv_backlight_turn_on(1); //move to mmi_usb_handle_plug_in
    if (!mmi_frm_scrn_enter(mmi_usb_get_normal_group(), SCR_DEV_USBDETECT, NULL, mmi_usb_entry_usb_detect_query_scrn_ind, 0))
    {
        return;
    }

    guiBuffer = mmi_frm_scrn_get_active_gui_buf();
#if defined(__MMI_MAINLCD_176X220__) || defined(__MMI_MAINLCD_240X320__)        /* 041205 Calvin modified */
    entry_full_screen();
#endif

    /* USB split revise*/
    /* EntryNewScreen will clear all key handler */
    //MMI_USB_CTX(EndKeyFuncPtr) = GetKeyHandler(KEY_END, KEY_EVENT_DOWN); //remove it
    
    /* Set mmi usb status */
    srv_usb_set_status(SRV_USBSTATUS_CONF_PAGE);

#if (defined(__MMI_USB_CDCACM_ONLY__) || defined(__MMI_USB_MS_ONLY__))
    {
        U16 str_title;

    #ifdef __MMI_USB_CDCACM_ONLY__

        #ifdef __USB_COM_PORT_ENABLE__
        /* CDCACM Mode only */
        highlightedItemIndex = MMI_USB_CONFIG_MENU_CDCACM;
        str_title = STR_USB_CONFIG_CDCACM_ONLY;
        #endif

    #else /* __MMI_USB_CDCACM_ONLY__ */ /* __MMI_USB_MS_ONLY__ */

        /* MS Mode only */
        #ifdef __USB_MASS_STORAGE_ENABLE__        
        highlightedItemIndex = MMI_USB_CONFIG_MENU_MS;
        str_title = STR_USB_CONFIG_MS_ONLY;
        #endif

    #endif /* __MMI_USB_CDCACM_ONLY__ */ 

        ShowCategory165Screen(
            STR_GLOBAL_YES,
            IMG_GLOBAL_YES,
            STR_GLOBAL_NO,
            IMG_GLOBAL_NO,
            get_string(str_title),
            mmi_get_event_based_image(MMI_EVENT_QUERY),
            0);
	SetLeftSoftkeyFunction(UsbDetectScrLskHdlr, KEY_EVENT_UP);
       SetCenterSoftkeyFunction(UsbDetectScrLskHdlr, KEY_EVENT_UP);
       SetRightSoftkeyFunction(UsbDetectScrRskHdlr, KEY_EVENT_UP);
       SetKeyHandler(UsbDetectScrLskHdlr, KEY_ENTER, KEY_EVENT_DOWN);
       ChangeCenterSoftkey(0, IMG_GLOBAL_COMMON_CSK);
    }

#else /* (defined(__MMI_USB_CDCACM_ONLY__) || defined(__MMI_USB_MS_ONLY__)) */ 

    /* Both Mass Storage and CDCACM mode */
    if (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_NORMAL || 
        srv_bootup_is_exception_mode())
    {

        mmi_usb_enter_detect_query_list();
        //mmi_frm_scrn_close(mmi_usb_get_normal_group(), SCR_DEV_USBDETECT);
    }
    else
    {
        /* If alarm/charger power on, display MS config confirm */
        ShowCategory165Screen(
            STR_GLOBAL_YES,
            IMG_GLOBAL_YES,
            STR_GLOBAL_NO,
            IMG_GLOBAL_NO,
            get_string(STR_USB_CONFIG_MS_ONLY),
            mmi_get_event_based_image(MMI_EVENT_QUERY),
            0);
		
        SetLeftSoftkeyFunction(UsbDetectScrLskHdlr, KEY_EVENT_UP);
        SetCenterSoftkeyFunction(UsbDetectScrLskHdlr, KEY_EVENT_UP);
        SetRightSoftkeyFunction(UsbDetectScrRskHdlr, KEY_EVENT_UP);
        SetKeyHandler(UsbDetectScrLskHdlr, KEY_ENTER, KEY_EVENT_DOWN);
        ChangeCenterSoftkey(0, IMG_GLOBAL_COMMON_CSK);
    }

#endif /* (defined(__MMI_USB_CDCACM_ONLY__) || defined(__MMI_USB_MS_ONLY__)) */ 

    /* USB split revise*/
    //SetKeyHandler(UsbDetectScrENDKeyHdlr, KEY_END, KEY_EVENT_DOWN); //remove it
    mmi_frm_scrn_set_leave_proc(mmi_usb_get_normal_group(), SCR_DEV_USBDETECT, mmi_usb_detect_cfg_leave_proc);
    
#ifdef __MMI_FM_RADIO__
    mmi_fmrdo_search_all_channels_stop();
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  ExitUsbDetectQueryScr
 * DESCRIPTION
 *  This function is exit handler for  "Usb Config:" screen
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: none(?)
 *****************************************************************************/
void ExitUsbDetectQueryScr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_EXIT_DETECT_QUERY_SCR);
#if defined(__MMI_MAINLCD_176X220__) || defined(__MMI_MAINLCD_240X320__)        /* 041205 Calvin modified */
    leave_full_screen();
#endif    
}


/*****************************************************************************
 * FUNCTION
 *  UsbDetectScrENDKeyHdlr
 * DESCRIPTION
 *  This function is END key handler at usb config page.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *****************************************************************************/
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

/*****************************************************************************
 * FUNCTION
 *  UsbDetectScrRskHdlr
 * DESCRIPTION
 *  This function is right softkey handler at usb config page.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *****************************************************************************/
void UsbDetectScrRskHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (GetScreenCountInHistory())
    {
        mmi_frm_scrn_close_active_id();
        mmi_usb_close_detect_query_list();
        srv_usb_set_status(SRV_USBSTATUS_IGNORED);
    }
    else
    {
        srv_shutdown_exit_system(0);
    }
}


/*****************************************************************************
 * FUNCTION
 *  UsbDetectScrLskHdlr
 * DESCRIPTION
 *  This function is left softkey handler at usb config page.
 *  (Press left softkey means start to config usb device as certain function)
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: highlightedItemIndex, the usb function index(?)
 *****************************************************************************/
void UsbDetectScrLskHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (srv_usb_is_dm_lock())
    {
        mmi_usb_display_popup(STR_GLOBAL_INVALID, MMI_EVENT_FAILURE);
        return;
    }
#ifdef __MMI_DSM_NEW__
	if(MR_SUCCESS == mr_app_system_event_hdlr(MR_SYSMSG_ENTER_USB, 0, 0))
		return;
#endif


    switch (highlightedItemIndex)
    {
    #ifdef __USB_MASS_STORAGE_ENABLE__        
        case MMI_USB_CONFIG_MENU_MS:    /* MASS Storage */
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_MS);
            srv_usb_set_owner(SRV_USB_MS_STORAGE_MS);            
            mmi_usb_config_first_page(DEVUSB_CFG_ACTION_MASS_STORAGE);
            break;
    #endif /* #ifdef __USB_MASS_STORAGE_ENABLE__ */
    
    #ifdef __USB_COM_PORT_ENABLE__
        case MMI_USB_CONFIG_MENU_CDCACM:    /* Com port emulator */
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_CDCACM);
            srv_usb_set_owner(SRV_USB_COMPORT_PS);
            mmi_usb_config_first_page(DEVUSB_CFG_ACTION_CDC_ACM);
            break;
    #endif
       

        default:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_NOTHING);
            mmi_frm_scrn_close_active_id();
            break;
    }
}



/*****************************************************************************
 * FUNCTION
 *  mmi_usb_config_cdc_acm
 * DESCRIPTION
 *  Display transition screen and send usb config request (MSG_ID_MMI_EQ_USBCONFIG_REQ) to HW
 *  
 *  PARAMETERS: mode, ms or cdcacm
 *  mode        [IN]        
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void mmi_usb_config_cdc_acm(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 ps_port;
    U32 ps_baudrate;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_IRDA_SUPPORT__
    if (mmi_irda_is_ircomm_active() == MMI_TRUE)
    {
        /* currently, when IrComm is active, switch port not allow! */
        mmi_usb_display_popup(STR_ID_USB_IRCOMM_OCCUPY_CDCACM, MMI_EVENT_FAILURE);

        mmi_usb_close_detect_query_list();
        return;
    }
#endif /* __MMI_IRDA_SUPPORT__ */ 

        nvram_get_ps(&ps_port, &ps_baudrate);

        if (ps_port == uart_port_null)
        {
            mmi_usb_display_popup(STR_ID_USB_UART2_OCCUPIED, MMI_EVENT_FAILURE);
            mmi_usb_close_detect_query_list();
            return;
        }

    mmi_frm_scrn_enter(GRP_ID_USB_MMI, GLOBAL_SCR_DUMMY, NULL, NULL, 0);
    mmi_usb_close_detect_query_list();
/*
    ClearKeyEvents();
    ClearInputEventHandler(MMI_DEVICE_KEY);
    ClearKeyHandler(KEY_END, KEY_EVENT_UP);
    ClearKeyHandler(KEY_END, KEY_EVENT_DOWN);
    ClearKeyHandler(KEY_END, KEY_LONG_PRESS);
*/
	   /* avoid entry UART setting screen */
   #ifdef __MMI_ENGINEER_MODE__
	   DeleteScreenIfPresent(EM_DEV_INLINE_CONFIG_UART_MENU_SCR);
	   DeleteScreenIfPresent(EM_DEV_SET_UART_MENU_SCR);
   #endif /* __MMI_ENGINEER_MODE__ */ 

    srv_usb_set_status(SRV_USBSTATUS_CDCACM_PROC);
    mmi_usb_display_popup(STR_USB_CDCACM_SETTING_SCR, MMI_EVENT_PROGRESS);  

#ifdef __MMI_USB_MULTIPLE_COMPORT_SUPPORT__
    #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
        if (srv_usb_get_owner() == SRV_USB_COMPORT_JAVA)
        {
            srv_usb_send_config_to_hw(DEVUSB_CFG_ACTION_CDC_ACM);
        }
        else
    #endif
        srv_usb_send_config_to_hw(DEVUSB_CFG_ACTION_MULTI_COM);
#else
        srv_usb_send_config_to_hw(DEVUSB_CFG_ACTION_CDC_ACM);
#endif

}

/*****************************************************************************
 * FUNCTION
 *  mmi_usb_config_first_page
 * DESCRIPTION
 *  Display transition screen and send usb config request (MSG_ID_MMI_EQ_USBCONFIG_REQ) to HW
 *  
 *  PARAMETERS: mode, ms or cdcacm
 *  mode        [IN]        
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
static MMI_BOOL mmi_usb_check_sys_ready()
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	
#ifdef WAP_SUPPORT
		if (mmi_bootup_is_network_service_available() == MMI_TRUE)
		{
			if (!wap_is_ready())
			{							
				return MMI_FALSE;
			}
		}
#endif
	
#ifdef MMS_SUPPORT
		if (mmi_bootup_is_network_service_available() == MMI_TRUE)
		{
			if (!mma_is_ready_for_usb())
			{
				return MMI_FALSE;
			}
		}
#endif /* MMS_SUPPORT */

    return MMI_TRUE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_config_first_page
 * DESCRIPTION
 *  Display transition screen and send usb config request (MSG_ID_MMI_EQ_USBCONFIG_REQ) to HW
 *  
 *  PARAMETERS: mode, ms or cdcacm
 *  mode        [IN]        
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
static void mmi_usb_config_first_page(U8 mode)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_CONF_FIRST_PAGE, mode);

    /* USB split revise*/
//#ifndef __MMI_BACKGROUND_CALL__ 
	if (srv_ucm_query_call_count(SRV_UCM_CALL_STATE_ALL, SRV_UCM_CALL_TYPE_ALL, NULL) > 0)
    {
        mmi_usb_display_popup(STR_GLOBAL_CURRENTLY_NOT_AVAILABLE_IN_CALL, MMI_EVENT_FAILURE);
        mmi_usb_close_detect_query_list();
        return;
    }
//#endif 

    if (IsScreenPresent(SCR_RESTORE_PROCESSING))
    {
        mmi_usb_display_popup(STR_GLOBAL_CURRENTLY_NOT_AVAILABLE, MMI_EVENT_FAILURE);
        mmi_usb_close_detect_query_list();
        return;
    }
    
    /* USB split revise*/
    mmi_frm_display_dummy_screen_ex(mmi_usb_get_normal_group(), GLOBAL_SCR_DUMMY);
    mmi_usb_close_detect_query_list();
    /* remove it
    ClearKeyEvents();
    ClearInputEventHandler(MMI_DEVICE_KEY);
    ClearKeyHandler(KEY_END, KEY_EVENT_UP);
    ClearKeyHandler(KEY_END, KEY_EVENT_DOWN);
    ClearKeyHandler(KEY_END, KEY_LONG_PRESS);
    */

#ifdef __USB_IN_NORMAL_MODE__

    #if defined(__MSDC_NOT_SUPPORT_HOT_PLUG__) || defined(__MSDC_TFLASH_DAT3_1BIT_HOT_PLUG__)
    if (!srv_usb_is_any_exported_drive())
    {
        mmi_usb_display_popup(STR_ID_USB_NO_MS_FOR_PC, MMI_EVENT_FAILURE);
        mmi_usb_close_progress_screen();
        srv_usb_set_status(SRV_USBSTATUS_IGNORED);
        return;
    }
    #endif /* defined(__MSDC_NOT_SUPPORT_HOT_PLUG__) || defined(__MSDC_TFLASH_DAT3_1BIT_HOT_PLUG__)*/

    if (!mmi_usb_need_to_reboot())
    {
        mmi_usb_entry_progress_screen();
		
		if (!mmi_usb_check_sys_ready())
		{
		     mmi_usb_display_popup(STR_ID_USB_SYS_NOT_READY, MMI_EVENT_FAILURE);
             mmi_usb_close_progress_screen();
             srv_usb_set_status(SRV_USBSTATUS_IGNORED);

            return;
		}
        else
        {
            srv_usb_set_status(SRV_USBSTATUS_MS_PROC);
            srv_usb_enter_ms_proc();
        }
     }
     else
#endif /* __USB_IN_NORMAL_MODE__*/
    {
        ShowCategory63Screen(
            (PU8) GetString(STR_USB_SHUTDOWN_SCR),
            mmi_get_event_based_image(MMI_EVENT_PROGRESS),
            NULL);  
        if ((srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_USB) || 
            (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_ALARM) ||
            (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_CHARGE))
        {
            srv_usb_start_ms_shutdown();
            srv_usb_set_status(SRV_USBSTATUS_MS_PROC);
        }
        else
        {
            /* USB split revise*/
            srv_usb_send_config_to_hw(mode);
            srv_usb_set_status(SRV_USBSTATUS_MS_PROC);
        }
    }      
}





/* ===========================================================================================  */
/* ===  Send Message to Protocol Stack  */
/* ===========================================================================================  */


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_progress_del_callback
 * DESCRIPTION
 *  Callback function of delete bmi screen
 * PARAMETERS
 *  p       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
static U8 mmi_usb_progress_del_callback(void *p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_USB_STATUS usb_state = srv_usb_get_status();

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (usb_state == SRV_USBSTATUS_MS_PROC)
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_entry_progress_screen
 * DESCRIPTION
 *  USB progress screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_entry_progress_screen(void)
{
    mmi_usb_enter_normal_group();
    mmi_usb_entry_progress_screen_int();
}

static void mmi_usb_entry_progress_screen_int(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_entry_progress_screen_int");	
    mmi_frm_scrn_close(mmi_usb_get_normal_group_id(), SCR_ID_USB_PROGRESS);
	
    if (!mmi_frm_scrn_enter(GRP_ID_USB_MMI, SCR_ID_USB_PROGRESS, NULL, mmi_usb_entry_progress_screen_int, 0))
    {
        return;
    }
    ShowCategory66Screen(
        STR_USB_CONFIG_TITLE,
        0,
        0,
        0,
        0,
        0,
        (PU8) GetString(STR_GLOBAL_PLEASE_WAIT),
        mmi_get_event_based_image(MMI_EVENT_PROGRESS),
        NULL);
    
    mmi_frm_scrn_set_leave_proc(GRP_ID_USB_MMI, SCR_ID_USB_PROGRESS, mmi_usb_progress_leave_proc);
}

static mmi_ret mmi_usb_progress_leave_proc(mmi_event_struct *param)
{
    SRV_USB_STATUS usb_state = srv_usb_get_status();

	kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_progress_leave_proc");	
	
    switch(param->evt_id)
    {
        case EVT_ID_SCRN_DELETE_REQ:
        case EVT_ID_SCRN_DELETE_REQ_IN_END_KEY:
        case EVT_ID_SCRN_GOBACK_IN_END_KEY:
            if (usb_state == SRV_USBSTATUS_MS_PROC)
            {
                return MMI_RET_ERR;
            }
            else
            {
                return MMI_RET_ALLOW_CLOSE;
            }
            
        default:
            return MMI_RET_ALLOW_CLOSE;
    }
    return MMI_RET_ALLOW_CLOSE; 
}

/*****************************************************************************
 * FUNCTION
 *  mmi_usb_close_progress_screen
 * DESCRIPTION
 *  Close USB progress screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_close_progress_screen(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_frm_scrn_close(mmi_usb_get_normal_group_id(), SCR_ID_USB_PROGRESS);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_app_unavailable_popup
 * DESCRIPTION
 *  To make a warning if the app. is disable in MS mode
 * PARAMETERS
 *  stringId        [IN]        0 will popup "USB: Can't access the storage now"; app specific string otherwise
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_usb_app_unavailable_popup(U16 stringId)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_usb_enter_normal_group();
    if (stringId == 0)
    {
        mmi_usb_display_popup(STR_ID_USB_MODE_EXPORTED_WARNING, MMI_EVENT_FAILURE);
    }
    else
    {
        mmi_usb_display_popup(stringId, MMI_EVENT_FAILURE);
    }

}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_get_error_info
 * DESCRIPTION
 *  Function to get the error information includes popup type and string
 * PARAMETERS
 *  error_result:   [IN]    error result type
 *  popup_type:     [OUT]   get the error popup type
 * RETURNS
 *  Get the error popup string id.
 *****************************************************************************/
MMI_STR_ID mmi_usb_get_error_info(S32 error_result, mmi_event_notify_enum* popup_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_STR_ID  str_id = STR_ID_USB_MODE_EXPORTED_WARNING;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    *popup_type = MMI_EVENT_FAILURE;
    if (error_result == MMI_USB_ERR_IS_UNAVAILABLE)
    {
        str_id = STR_ID_USB_MODE_EXPORTED_WARNING;
    }
    return str_id;
}

mmi_ret mmi_usb_config_rsp_callback(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 mode = *(U8*)evt->user_data;
	srv_usb_config_result_struct *result = (srv_usb_config_result_struct*)evt->user_data;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_config_rsp_callback: mode %d, suc %d", 
	    result->mode, result->suc);
	
	if (!result->suc)
	{
	    mmi_usb_display_popup(STR_GLOBAL_ERROR, MMI_EVENT_FAILURE);
	    mmi_usb_close_progress_screen();           
        mmi_usb_close_detect_query_list();
	    return MMI_RET_OK;
	}
	
    switch (result->mode)
    {
        case DEVUSB_CFG_ACTION_MASS_STORAGE:
    #ifdef __MTP_ENABLE__			
        case DEVUSB_CFG_ACTION_MTP:
    #endif
        {
        #ifdef __MMI_BACKGROUND_CALL__
                if ((srv_ucm_query_call_count(SRV_UCM_INCOMING_STATE, SRV_UCM_CALL_TYPE_ALL, NULL) > 0) ||
                    (srv_ucm_query_call_count(SRV_UCM_OUTGOING_STATE, SRV_UCM_CALL_TYPE_ALL, NULL) > 0))
                {
                    U16 marker_screen = 0, start_screen = 0, end_screen = 0;

                    marker_screen = mmi_ucm_get_marker_scrn_id();
                    GetPreviousScrnIdOf(marker_screen, &end_screen);
                    GetNextScrnIdOf(IDLE_SCREEN_ID, &start_screen);
                    DeleteBetweenScreen(start_screen, end_screen);
                    mmi_usb_close_progress_screen();
                }
                else
                {
                    mmi_idle_display();
                }
        #else
                if (srv_ucm_query_call_count(SRV_UCM_CALL_STATE_ALL, SRV_UCM_CALL_TYPE_ALL, NULL) == 0)
                {
                	kal_prompt_trace(MOD_MMI, "[USB MMI] no call");
                    mmi_idle_display();
                }
                else
                {
                    U16 marker_screen = 0, start_screen = 0, end_screen = 0;

			kal_prompt_trace(MOD_MMI, "[USB MMI] call exists");
                    marker_screen = mmi_ucm_get_marker_scrn_id();

                    GetPreviousScrnIdOf(marker_screen, &end_screen);
                    GetNextScrnIdOf(IDLE_SCREEN_ID, &start_screen);
                    DeleteBetweenScreen(start_screen, end_screen);
                    mmi_usb_close_progress_screen();
                }
         #endif /* #ifndef __MMI_BACKGROUND_CALL__ */

            #ifdef __USB_IN_NORMAL_MODE__
                if (!srv_usb_is_any_exported_drive())
                {
                    //mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_NO_MS_FOR_PC), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
					mmi_frm_nmgr_popup(MMI_SCENARIO_ID_DEFAULT, MMI_EVENT_INFO, (WCHAR*)GetString(STR_ID_USB_NO_MS_FOR_PC));
                }
                else
            #endif
                {
                    //mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS, mmi_usb_get_normal_group(), NULL);
					mmi_frm_nmgr_popup(MMI_SCENARIO_ID_DEFAULT, MMI_EVENT_SUCCESS, (WCHAR*)GetString(STR_GLOBAL_DONE));
                }
                
                mmi_usb_close_progress_screen();
				mmi_usb_close_detect_query_list();

				break;

		#ifdef __MMI_PICT_BRIDGE_SUPPORT__
        case DEVUSB_CFG_ACTION_PICTBRIGE: /* PictBrige */
            mmi_usb_display_popup(STR_GLOBAL_DONE, MMI_EVENT_SUCCESS);
            mmi_usb_close_detect_query_list();
            break;
        #endif /* __MMI_PICT_BRIDGE_SUPPORT__ */

        case DEVUSB_CFG_ACTION_STOP_MS:
        case DEVUSB_CFG_ACTION_STOP_MTP:
        case DEVUSB_CFG_ACTION_WEBCAM:
            break;
            
        default:
            mmi_usb_display_popup(STR_GLOBAL_DONE, MMI_EVENT_SUCCESS);
            mmi_usb_close_progress_screen();
            mmi_usb_close_detect_query_list();
            break;
        }
    }
    return MMI_RET_OK;
}

mmi_ret mmi_usb_mmi_notify_callback(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    SRV_USB_MMI_NOTIFY_ACTION act = (SRV_USB_MMI_NOTIFY_ACTION)evt->user_data;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch(act)
    {
        case SRV_USB_MMI_NOTIFY_POWER_OFF_REMOVED:
            //BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
            //mmi_frm_scrn_close(mmi_usb_get_boot_group_id(), SCR_USB_POWER_ON);
            //mmi_alm_enable_pwroff();
            mmi_usb_close_power_off_screen();
            break;

        case SRV_USB_MMI_NOTIFY_REMOVED:
            BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
            break;
            
        case SRV_USB_MMI_NOTIFY_CLOSE_CFG_LIST:
            mmi_usb_close_detect_query_list();
            break;

        case SRV_USB_MMI_NOTIFY_CLOSE_PROGRESS:
            mmi_usb_close_progress_screen();
            break;

        case SRV_USB_MMI_NOTIFY_EXIT_WEBCAM:
        #ifdef __MMI_WEBCAM__
            mmi_usb_close_detect_query_list();
            mmi_usb_exit_webcam();
        #endif
            break;

        case SRV_USB_MMI_NOTIFY_CFG_ERROR:
            mmi_usb_display_popup(STR_GLOBAL_ERROR, 
                MMI_EVENT_FAILURE);
            break;
            
        default:
            break;
    }

    return MMI_RET_OK;
}

/* Screen group*/
static MMI_ID g_usb_normal_mode_group = GRP_ID_INVALID;
static MMI_ID g_query_list_menu_id = GRP_ID_INVALID;

static void mmi_usb_enter_normal_group(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables												  */
	/*----------------------------------------------------------------*/
    MMI_ID gid;

	/*----------------------------------------------------------------*/
	/* Code Body													  */
	/*----------------------------------------------------------------*/
    kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_enter_normal_group, %d", g_usb_normal_mode_group);
    if (g_usb_normal_mode_group != GRP_ID_INVALID)
    {
        return;
    }
    gid = mmi_frm_group_create(GRP_ID_ROOT, GRP_ID_USB_MMI, mmi_usb_normal_group_proc, NULL);
    mmi_frm_group_enter(gid, MMI_FRM_NODE_SMART_CLOSE_FLAG);

    g_usb_normal_mode_group = gid;
}

static MMI_ID mmi_usb_get_normal_group(void)
{
    mmi_usb_enter_normal_group();
    return g_usb_normal_mode_group;
}

static MMI_ID mmi_usb_get_normal_group_id(void)
{
    if (g_usb_normal_mode_group == GRP_ID_INVALID)
    {
        kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_get_normal_group_id, group not exist");
        return GRP_ID_USB_MMI;
    }
    return g_usb_normal_mode_group;
}

static mmi_ret mmi_usb_normal_group_proc(mmi_event_struct *param)
{
    cui_menu_event_struct *menu_evt;

    kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_normal_group_proc");

    menu_evt = (cui_menu_event_struct*)param;
    
    switch(menu_evt->evt_id)
    {
        case EVT_ID_GROUP_DEINIT:
            kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_GROUP_DEINIT");
            g_usb_normal_mode_group = GRP_ID_INVALID;
            break;
            
        case EVT_ID_CUI_MENU_LIST_ENTRY:
            mmi_usb_init_detect_query_list();
            break;
            
        case EVT_ID_CUI_MENU_ITEM_SELECT:
            kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_CUI_MENU_ITEM_SELECT");
            mmi_usb_query_list_select_hdlr(menu_evt);
            break;
            
        case EVT_ID_CUI_MENU_CLOSE_REQUEST:
            kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_CUI_MENU_CLOSE_REQUEST");
            //srv_usb_set_status(SRV_USBSTATUS_IGNORED);
            //cui_menu_close(menu_evt->sender_id);
            //g_query_list_menu_id = GRP_ID_INVALID;
            mmi_usb_close_detect_query_list();
            srv_usb_set_status(SRV_USBSTATUS_IGNORED);
            break;

        default:
            break;
    }

    return MMI_RET_OK;
}

static void mmi_usb_init_detect_query_list(void)
{
    S32 i;
    mmi_menu_id items[] = {
#ifdef __USB_MASS_STORAGE_ENABLE__
    	   MENU_ID_USB_MS,
#endif
#ifdef __MMI_WEBCAM__
    	   MENU_ID_USB_WEBCAM,
#endif
#ifdef __USB_COM_PORT_ENABLE__
    		MENU_ID_USB_CDCAM_DATA,
#endif
#ifdef __MMI_PICT_BRIDGE_SUPPORT__
    		MENU_ID_USB_PICTBRIDGE,
#endif
#if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
    		MENU_ID_USB_COMPORT_JAVA,
#endif
#ifdef  __MTP_ENABLE__
    		MENU_ID_USB_MTP,
#endif
    		0
    		};
    U16 strings[] = 
    {
#ifdef __USB_MASS_STORAGE_ENABLE__        
    STR_USB_CONFIG_MS,
#endif
#ifdef __MMI_WEBCAM__
    STR_ID_USB_WEBCAM,
#endif 
#ifdef __USB_COM_PORT_ENABLE__
    STR_USB_CONFIG_CDCACM_DATA,
#endif
#ifdef __MMI_PICT_BRIDGE_SUPPORT__
    STR_USB_CONFIG_PICT_BRIGE,
#endif
#if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
    STR_USB_CONFIG_COMPORT_JAVA,
#endif
#ifdef  __MTP_ENABLE__
    STR_USB_CONFIG_MTP,
#endif
        0
    };
    S32 count = sizeof(items) / sizeof(items[0]) - 1;

    cui_menu_set_currlist_title(g_query_list_menu_id, (WCHAR*)GetString(STR_USB_CONFIG_TITLE), NULL);
    cui_menu_set_currlist(g_query_list_menu_id, count, (MMI_MENU_ID*)items);
    cui_menu_set_currlist_left_softkey(g_query_list_menu_id, (WCHAR*)GetString(STR_GLOBAL_OK));
    cui_menu_set_currlist_right_softkey(g_query_list_menu_id, (WCHAR*)GetString(STR_GLOBAL_BACK));
    for (i = 0; i < count; i++)
    {
    	cui_menu_set_item_string(g_query_list_menu_id, items[i], (WCHAR*)GetString(strings[i]));
    }

    cui_menu_set_currlist_flags(g_query_list_menu_id, CUI_MENU_LIST_WITH_NO_MARGIN_WHEN_NO_ICON);
}

static void mmi_usb_enter_detect_query_list(void)
{
    g_query_list_menu_id = cui_menu_create(mmi_usb_get_normal_group(), 
        CUI_MENU_SRC_TYPE_APPCREATE, 
        CUI_MENU_TYPE_APPSUB, 
        0, 
        MMI_FALSE, 
        NULL);

    cui_menu_run(g_query_list_menu_id);
}

static void mmi_usb_query_list_select_hdlr(cui_menu_event_struct *event)
{
    kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_query_list_select_hdlr");
    
    if (srv_usb_is_dm_lock())
    {
        kal_prompt_trace(MOD_MMI, "[USBMMI] lock");
        mmi_usb_display_popup(STR_GLOBAL_INVALID, MMI_EVENT_FAILURE);
        return;
    }

    if ( (srv_usb_get_status() == SRV_USBSTATUS_PLUGOUT))
	{
		mmi_usb_display_popup(STR_GLOBAL_ERROR, MMI_EVENT_FAILURE);
		mmi_usb_close_detect_query_list();
		return;
	}
	
    kal_prompt_trace(MOD_MMI, "[USBMMI] menu_id=%d", event->highlighted_menu_id);
#ifdef __MMI_DSM_NEW__
	if(MR_SUCCESS == mr_app_system_event_hdlr(MR_SYSMSG_ENTER_USB, 0, 0))
		return;
#endif

    switch(event->highlighted_menu_id)
    {
    #ifdef __USB_MASS_STORAGE_ENABLE__
        case MENU_ID_USB_MS:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_MS);
            srv_usb_set_owner(SRV_USB_MS_STORAGE_MS);
            mmi_usb_config_first_page(DEVUSB_CFG_ACTION_MASS_STORAGE);
            break;
    #endif

    #ifdef __MMI_WEBCAM__
	
        case MENU_ID_USB_WEBCAM:
		//	MMI_ASSERT(mmi_usb_ptr->webcam_state == MMI_USB_WEBCAM_DISCONNECT);

        //    mmi_usb_ptr->webcam_state = MMI_USB_WEBCAM_CONNECTED;

            //mmi_usb_webcam_entry_app_scr();
            //mmi_usb_close_detect_query_list();    
            mmi_usb_config_webcam();
            break;
    #endif
    
    #ifdef __USB_COM_PORT_ENABLE__
        case MENU_ID_USB_CDCAM_DATA:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_CDCACM);
            srv_usb_set_owner(SRV_USB_COMPORT_PS);
    		mmi_usb_config_cdc_acm();
            break;
    #endif
    
    #ifdef __MMI_PICT_BRIDGE_SUPPORT__
        case MENU_ID_USB_PICTBRIDGE:

        #ifdef MMI_ON_HARDWARE_P

            srv_usb_set_status(SRV_USBSTATUS_PICTBRIGE);
			mmi_usb_display_popup(STR_USB_PICT_BRIGE_CONFIGING, MMI_EVENT_PROGRESS);
			srv_usb_send_config_to_hw(DEVUSB_CFG_ACTION_PICTBRIGE);      
			
        #else /* MMI_ON_HARDWARE_P */ 
            mmi_usb_close_detect_query_list();
        #endif /* MMI_ON_HARDWARE_P */   

            break;
    #endif
    
    #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
        case MENU_ID_USB_COMPORT_JAVA:
            srv_usb_set_owner(SRV_USB_COMPORT_JAVA);
            mmi_usb_config_cdc_acm();
            break;
    #endif
    
    #ifdef  __MTP_ENABLE__
        case MENU_ID_USB_MTP:
            srv_usb_set_owner(SRV_USB_MS_STORAGE_MTP);
            mmi_usb_config_first_page(DEVUSB_CFG_ACTION_MTP);
            break;
    #endif

        default:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_NOTHING);
            mmi_usb_close_detect_query_list();
            break;
    }
    //cui_menu_close(event->sender_id);
    //g_query_list_menu_id = GRP_ID_INVALID;
}

static void mmi_usb_close_detect_query_list(void)
{
    kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_close_detect_query_list");
    mmi_frm_scrn_close(mmi_usb_get_normal_group_id(), SCR_DEV_USBDETECT);
    if (g_query_list_menu_id != GRP_ID_INVALID)
    {
        cui_menu_close(g_query_list_menu_id);
        g_query_list_menu_id = GRP_ID_INVALID;
    }
}

static mmi_ret mmi_usb_detect_cfg_leave_proc(mmi_event_struct *param)
{
    SRV_USB_STATUS usb_state = srv_usb_get_status();

    kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_detect_cfg_leave_proc, state=%d", usb_state);

    switch(param->evt_id)
    {
        case EVT_ID_SCRN_GOBACK_IN_END_KEY:
        case EVT_ID_SCRN_DELETE_REQ_IN_END_KEY:
            srv_usb_set_status(SRV_USBSTATUS_IGNORED);
            break;

        default:
            break;
    }
    return MMI_RET_ALLOW_CLOSE; 
}

#ifdef __USB_IN_NORMAL_MODE__
/*****************************************************************************
 * FUNCTION
 *  mmi_usb_need_to_reboot
 * DESCRIPTION
 *  Check if it it necessary to reboot for USB mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static MMI_BOOL mmi_usb_need_to_reboot(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

#ifdef __USB_IN_NORMAL_MODE_IMPROVE__
    if (AlmIsRTCPwron())
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }    
#elif !defined(__FS_CARD_SUPPORT__) && !defined(NAND_SUPPORT)
    return MMI_TRUE;
#elif !defined(__FS_CARD_SUPPORT__) && defined(__NOR_NOT_PRESENT__)
    return MMI_TRUE;
#else 
    if (AlmIsRTCPwron())
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }
#endif 
}

#endif

#ifdef __MMI_WEBCAM__

static void mmi_usb_config_webcam(void)
{
#ifndef __MMI_BACKGROUND_CALL__ 
	if (srv_ucm_query_call_count(SRV_UCM_CALL_STATE_ALL, SRV_UCM_CALL_TYPE_ALL, NULL) > 0)
    {
        mmi_usb_display_popup(STR_GLOBAL_CURRENTLY_NOT_AVAILABLE_IN_CALL, MMI_EVENT_FAILURE);
        mmi_usb_close_detect_query_list();
        return;
    }
#endif 
    if (IsScreenPresent(SCR_RESTORE_PROCESSING))
    {
        mmi_usb_display_popup(STR_GLOBAL_CURRENTLY_NOT_AVAILABLE, MMI_EVENT_FAILURE);
        mmi_usb_close_detect_query_list();
        return;
    }
    mmi_webcam_enter();
    mmi_usb_close_detect_query_list();
}

static void mmi_usb_exit_webcam(void)
{
    mmi_webcam_exit();
}

#endif /* __MMI_WEBCAM__*/

static void mmi_usb_display_popup(U16 string, mmi_event_notify_enum type)
{
    mmi_frm_nmgr_popup(MMI_SCENARIO_ID_DEFAULT, type, (WCHAR*)GetString(string));
}

#else /* __MMI_USB_SPLIT_SUPPORT__*/

#include "bootup.h"

#include "AllAppGprot.h"
#include "ProtocolEvents.h"
#include "CommonScreens.h"      /* DisplayPopup */
#include "SimDetectionGexdcl.h" /* To access g_charbat_context */
#include "SimDetectionGprot.h"
#include "IdleAppProt.h"
#include "MMItaskProt.h"        /* InitFramework */
#include "vObjects.h"
#include "PowerOnChargerProt.h" /* BatteryIndicationPopup() */

#ifdef __USB_IN_NORMAL_MODE__
#include "WPSSProtos.h" /* for DispChar */
#include "FileMgr.h"

#ifdef __MMI_EMAIL__
#include "EmailAppGProt.h"
#endif /* __MMI_EMAIL__ */ 

#endif /* __USB_IN_NORMAL_MODE__ */ 

#include "FileMgrSrvGProt.h"
#include "FileManagerGProt.h"

#ifdef __MMI_IRDA_SUPPORT__
#include "IrdaMMIGprots.h"      /* mmi_irda_is_open() */
#endif 

#ifdef __MMI_MOBILE_TV_PLAYER__
#include "mobiletvplayergprot.h"
#endif
#include "NwUsabSrvGprot.h"

/* Trace Header Files */

/* USB Device Headder Files */
#include "ExtDeviceDefs.h"
#include "USBDeviceGprot.h"
#include "USBDeviceDefs.h"
#include "USBDeviceGexdcl.h"

#ifdef __MMI_ENGINEER_MODE__
#include "EngineerModeDef.h"
#endif 
#include "phonebookgprot.h"

#include "lcd_sw.h"
#include "uart_sw.h"
#include "lcd_sw.h"
#include "lcd_sw_inc.h"
#include "gpioInc.h"
#include "lcd_if.h"

#ifdef __MMI_WEBCAM__
#include "webcamprot.h"
#endif /* __MMI_WEBCAM__ */ 

#ifdef __SAT__
#include "SATResDef.h" 
#endif

#ifdef __MMI_MMS__
#include "wapadp.h"
#endif 

#if defined(__MMI_VIDEO_RECORDER__) && !defined(__MMI_CAMCORDER__)
#include "VdoRecGprot.h"
#endif 

#include "SettingDefs.h"        /* SCR_RESTORE_PROCESSING */
#include "SettingsGdcl.h"
#include "AlarmFrameWorkProt.h"
#include "AlarmProt.h"
#include "vObjectsResDef.h"
#include "datetimetype.h"
#include "app_datetime.h"

#include "nvram_enums.h"
#include "Nvram_interface.h"


#include "PhoneSetupGprots.h"

#ifdef WAP_SUPPORT
#include "Wapadp.h"
#endif

#ifdef __MMI_PICT_BRIDGE_SUPPORT__
#include "PictBridgeGProt.h"
#endif
 
#ifdef __MMI_DOWNLOAD_AGENT__
#include "FileMgr.h"
#include "DLAgentDef.h"
#include "DLAgentGProt.h"
#endif

#if defined(__MMI_OPP_SUPPORT__) && defined(__MMI_BT_MTK_SUPPORT__)
#include "BtcmSrvGprot.h"
#include "BTMMIOppGprots.h"
#endif /* defined(__MMI_OPP_SUPPORT__) && defined(__MMI_BT_MTK_SUPPORT__) */

#if defined(__MMI_BT_MTK_SUPPORT__)  && (defined(__MMI_FTS_SUPPORT__) || defined(__MMI_FTC_SUPPORT__))
#include "BtcmSrvGprot.h"
#include "BTMMIFtpGprots.h"
#endif

#if defined(__MMI_BIP_SUPPORT__) 
#include "BtcmSrvGprot.h"
#include "BTMMIBipGprot.h"
#endif

#ifdef __J2ME__
#include "jam_internal.h"
#endif

#include "Device.h"
#include "l4c_common_enum.h"

#ifdef __MMI_DUAL_SIM_MASTER__
#include "MTPNP_AD_master_header.h"
#endif /* __MMI_DUAL_SIM_MASTER__ */

#ifdef __MMI_MEDIA_PLAYER__
#include "MediaPlayerGProt.h"
#endif

#ifdef __PLST_SERVICE_DB_SUPPORT__
#include "PlstSrvGprot.h"
#endif

#include "UcmSrvGprot.h"
#include "UCMGProt.h"

#ifdef __J2ME__
#include "Jvm_interface.h"
#endif

#ifdef __VRE__
#include "vmopt.h"
#endif

#ifdef __VRE30_AM__
#include "AMGprot.h"
#endif

#if defined(__DM_LAWMO_SUPPORT__)
#include "mmiapi_dm_struct.h"
#endif

#include "Mmi_frm_scenario_gprot.h"
#include "mmi_rp_app_usbmmi_def.h"

#include "UcAppGprot.h"


#include "BootupSrvGprot.h"
#include "ShutdownSrvGprot.h"
#include "IdleGprot.h"

#include "ProfilesSrvGProt.h"
#ifdef __MMI_DSM_NEW__
#include "mrp_include.h"
#endif

/***************************************************************************** 
* Define
*****************************************************************************/
#ifdef __USB_IN_NORMAL_MODE__
#define MAX_DEINIT_CALLBACK   sizeof(mmi_usb_deinit_callback)/sizeof(mmi_usb_ms_callback_struct) - 1
#define MAX_REINIT_CALLBACK   sizeof(mmi_usb_reinit_callback)/sizeof(mmi_usb_ms_callback_struct) - 1
#endif /* __USB_IN_NORMAL_MODE__ */ 

/***************************************************************************** 
* Typedef
*****************************************************************************/
#ifdef __USB_IN_NORMAL_MODE__
typedef U8(*ChkFuncPtr) (void);

typedef struct
{
    U16 eventID;
    PsFuncPtr RspHdlr;
    FuncPtr callback;
    ChkFuncPtr checkfunc;
} mmi_usb_ms_callback_struct;
#endif /* __USB_IN_NORMAL_MODE__ */ 
/***************************************************************************** 
* Extern Variable
*****************************************************************************/
extern U8 subMenuData[MAX_SUB_MENUS][MAX_SUB_MENU_SIZE];
extern PU8 subMenuDataPtrs[MAX_SUB_MENUS];
extern charbat_context_struct g_charbat_context;

/***************************************************************************** 
* Extern Functions
*****************************************************************************/
extern void ClearKeyEvents(void);
extern void PopulateResData(void);
#ifdef __MMI_FRAMEWORK_BACKWARD_COMPATIBLE__
extern void InitUnicodeSupport(void);
#endif
extern U8 AlmIsRTCPwron(void);
extern void AlmDeInit(void);
extern void SetClamState(U16 clam_state);
extern void PhnsetReadGPIOSetting(void);

#ifdef __NVRAM_IN_USB_MS__
extern void InitHardwareEvents(void);
extern void InitSettingBootup(void);
extern void InitSettingNVRAM(void);
extern void PhnsetReadNvramCalibrationData(void);
extern void InitRingToneComposer(void);
extern void InitAlarmFrameWork(void);
extern void PendingAlarmReminder(void);
#endif /* __NVRAM_IN_USB_MS__ */ 

#ifdef __MMI_FM_RADIO__
extern void mmi_fmrdo_search_all_channels_stop(void);
#endif 

#ifdef __MMI_AUDIO_PLAYER__
extern void mmi_audply_stop_playing(void);
extern void mmi_audply_do_single_stop_action(void);
extern void mmi_audply_usb_plug_in_hdlr(void);
extern void mmi_audply_usb_plug_out_hdlr(void);
#endif /* __MMI_AUDIO_PLAYER__ */ 

#ifdef __MMI_LANGLN__
extern void mmi_langln_util_stop_update(void);
#endif /*__MMI_LANGLN__*/

#ifdef __USB_IN_NORMAL_MODE__
#ifdef WAP_SUPPORT
extern void wap_on_enter_usb_mode(void);
#endif 
#ifdef MMS_SUPPORT
extern void mms_on_enter_usb_mode(void);
extern void mms_on_exit_usb_mode(void);
extern int mma_is_ready_for_usb(void);
#endif /* MMS_SUPPORT */ 

#ifdef __MMI_DOWNLOADABLE_THEMES_SUPPORT__
extern void mmi_tm_init_theme_usb_mode_plugin_callback(void);
extern void mmi_tm_init_theme_usb_mode_plugout_callback(void);
#endif /* __MMI_DOWNLOADABLE_THEMES_SUPPORT__ */ 
#ifdef __MMI_EBOOK_READER__
extern void mmi_ebook_usb_mode_on(void);
extern void mmi_ebook_usb_mode_off(void);
#endif /* __MMI_EBOOK_READER__ */ 

#ifdef __MMI_MMS_2__
extern void srv_mms_usb_mode_on(void);
extern void srv_mms_usb_mode_off(void);
#endif

#ifdef __MMI_MMS_BGSR_SUPPORT__
extern void mmi_mms_bgsr_usb_start_cb(void);
extern void mmi_mms_bgsr_usb_end_cb(void);
#endif /*__MMI_MMS_BGSR_SUPPORT__*/
//extern void mmi_cbm_usb_delete_confirm_callback(void);

#ifdef __MMI_BROWSER_2__
extern void mmi_brw_app_deinit(void);
extern MMI_BOOL mmi_brw_is_browser_active(void);
#endif

#ifdef __A8BOX_SUPPORT__
extern void mmi_a8box_enter_usb_mode(void);
extern void mmi_a8box_exit_usb_mode(void);
#endif /* __A8BOX_SUPPORT__ */

#ifdef __QQIM_SUPPORT__
extern void ExitQQ( void );
#endif

#ifdef __MTP_ENABLE__
extern kal_bool USB_Boot_With_MS(void);
#endif


#if defined(__DM_LAWMO_SUPPORT__)
extern MMI_BOOL mmi_dmui_is_phone_lock(void);
extern void mmi_usb_dmui_lock_notify(mmi_dmui_app_id_enum app_id, mmi_dmui_cmd_enum cmd);
MMI_BOOL g_mmi_usb_dmui_lock_process; /* 0:default, 1: dm lock */
#endif

#if defined(OPERA_V10_BROWSER) || defined(__GADGET_SUPPORT__)
extern void npplg_av_main_exit_usb_mode(void);
#endif

#ifdef OPERA_V10_BROWSER
extern void mmi_opera_brw_enter_usb_mode_hdlr(void);
extern BOOL mmi_opera_brw_usb_mode_check(void);
#endif

#ifdef __MMI_VUI_SHELL_IMAGE_VIEWER__
extern void vadp_shell_imgviewer_check_filelist(void);
#endif

#if defined(__MMI_GADGET_SUPPORT__)
extern void srv_wgtmgr_exit_usb_mode_hdlr(void);
#endif

#ifdef __VRE__
extern void mmi_vre_enter_usb_mode(void);
#endif

#endif /* __USB_IN_NORMAL_MODE__ */ 
/***************************************************************************** 
* Local Functions
*****************************************************************************/
#if defined (__USB_IN_NORMAL_MODE__)
static void mmi_usb_hide_file_related_menus(void);
static void mmi_usb_restore_file_related_menus(void);
static MMI_BOOL mmi_usb_need_to_reboot(void);
static void mmi_usb_entry_progress_screen(void);
static void mmi_usb_close_progress_screen(void);
static void mmi_usb_app_deinit(void *p);
static void mmi_usb_app_reinit(void *p);
static void mmi_usb_send_removal_event_rsp(void *info);
#ifdef MMS_SUPPORT
static U8 mmi_usb_mms_check_to_deinit(void);
#endif
static U8 mmi_usb_mms_check_to_reinit(void);
#endif /* defined (__USB_IN_NORMAL_MODE__) */ 
static void mmi_usb_send_removal_event_req(void);
static void mmi_usb_send_power_on_req(void);
static void mmi_usb_send_detect_ack(void);

static MMI_BOOL mmi_usb_pre_check_cfg_screen_permit(void);
static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display(void);
static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify(mmi_scenario_id scen_id, void *arg);
static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_test(void);
static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify_dummy(mmi_scenario_id scen_id, void *arg);

/* Screen group*/
static void mmi_usb_enter_usb_boot_group(void);
static void mmi_usb_close_usb_boot_group(void);
static mmi_ret mmi_usb_boot_group_proc(mmi_event_struct *param);
static void mmi_usb_enter_normal_group(void);
static MMI_ID mmi_usb_get_boot_group(void);
static MMI_ID mmi_usb_get_normal_group(void);
static mmi_ret mmi_usb_normal_group_proc(mmi_event_struct *param);
static void mmi_usb_show_usb_chg_cmplt_scrn_int(void);
static void mmi_usb_show_usb_scrn_int(void);
static void mmi_usb_entry_usb_detect_query_scrn_ind(void);
static void mmi_usb_poweron_finish_scrn_int(void);
static void mmi_usb_entry_progress_screen_int(void);
static mmi_ret mmi_usb_progress_leave_proc(mmi_event_struct *param);
static mmi_ret mmi_usb_detect_cfg_leave_proc(mmi_event_struct *param);

/* Menu CUI*/
//#define MMI_USB_MENU_CUI

#ifdef MMI_USB_MENU_CUI
#include "menucuigprot.h"
static void mmi_usb_enter_detect_query_list(void);
static void mmi_usb_init_query_list(MMI_ID menu_id);
static void mmi_usb_query_list_select_hdlr(cui_menu_event_struct *event);
#endif
static void mmi_usb_close_detect_query_list(void);
/***************************************************************************** 
* Local Variable
*****************************************************************************/
MMI_USB_CONTEXT g_mmi_usb_cntx;
MMI_USB_CONTEXT *mmi_usb_ptr = &g_mmi_usb_cntx;
static S32 highlightedItemIndex = 0;

#ifdef __USB_IN_NORMAL_MODE__
static MMI_BOOL mmi_usb_is_exported = 0;
static mmi_usb_ms_callback_struct mmi_usb_deinit_callback[] = 
{
    {0, NULL, PhnsetWPUseDefault, NULL},        /* suspend wallpaper */
    {0, NULL, PhnsetSSUseDefault, NULL},        /* suspend screensaver */
#ifdef __MMI_POWER_ON_OFF_DISPLAY__
    {0, NULL, PhnsetPwrOnOffUseDefault, NULL},  /* suspend power on/off disp. */
#endif 
#ifdef __MMI_AUDIO_PLAYER__
    {0, NULL, mmi_audply_stop_playing, NULL},
    {0, NULL, mmi_audply_do_single_stop_action, NULL},
    {0, NULL, mmi_audply_usb_plug_in_hdlr, NULL},
#endif /* __MMI_AUDIO_PLAYER__ */ 
#ifdef __MMI_MEDIA_PLAYER__
    {0, NULL, mmi_medply_plst_usb_plug_in_hdlr, NULL},
    {0, NULL, mmi_medply_stop_playing, NULL},
#endif /*__MMI_MEDIA_PLAYER__*/
#ifdef __PLST_SERVICE_DB_SUPPORT__
    {0, NULL, srv_plst_usb_plug_in_hdlr, NULL},
#endif
#ifdef __MMI_EMAIL__
    {0, NULL, mmi_email_deinit, NULL},
#endif 
    {0, NULL, srv_fmgr_drv_init, NULL},
#ifdef __MMI_DOWNLOAD_AGENT__
    {0, NULL, mmi_da_usb_mode_on, NULL},
#endif
#ifdef MMS_SUPPORT
    {MSG_ID_MMS_ENTER_USB_MODE_RSP, NULL, mms_on_enter_usb_mode, mmi_usb_mms_check_to_deinit},
#endif 
#if defined(__MMI_VIDEO_RECORDER__) && !defined(__MMI_CAMCORDER__)
    {0, NULL, mmi_vdorec_usb_mode_hdlr, NULL},
#endif 
#ifdef __MMI_DOWNLOADABLE_THEMES_SUPPORT__
    {0, NULL, mmi_tm_init_theme_usb_mode_plugin_callback, NULL},
#endif 
#ifdef __MMI_EBOOK_READER__
    {0, NULL, mmi_ebook_usb_mode_on, NULL},
#endif 
#ifdef __MMI_IRDA_SUPPORT__
    {0, NULL, mmi_irda_obex_snd_abort, mmi_irda_is_sending},
#endif
#ifdef __MMI_MMS_2__
    {0, NULL, srv_mms_usb_mode_on, NULL},
#endif
#ifdef __MMI_MMS_BGSR_SUPPORT__
    {0, NULL, mmi_mms_bgsr_usb_start_cb, NULL},
#endif  /*__MMI_MMS_BGSR_SUPPORT__*/
#if defined(__MMI_OPP_SUPPORT__) && defined(__MMI_BT_MTK_SUPPORT__)
    {0, NULL, mmi_opp_usb_plugin_hdlr, NULL},
#endif

    /* call back when plug in usb mass*/
#if defined(__MMI_BT_MTK_SUPPORT__)  && (defined(__MMI_FTS_SUPPORT__) || defined(__MMI_FTC_SUPPORT__))
    {0, NULL, mmi_bt_ftp_usb_plugin_hdle, mmi_bt_ftp_usb_plugin_check},
#endif

#ifdef __MMI_BIP_SUPPORT__
    {MSG_ID_BT_BIP_ENTER_USB_MODE_CNF, mmi_bt_bip_usb_mode_cnf_hdlr, mmi_bt_bip_enter_usb_mode_hdlr, mmi_bt_bip_usb_mode_check},
#endif

#ifdef __J2ME__
    {0, NULL, jam_enter_usb_mode, (ChkFuncPtr)jam_usb_mode_check},
#endif

#ifdef __MMI_LANGLN__
    {0, NULL, mmi_langln_util_stop_update, NULL},
#endif

#ifdef __MMI_MOBILE_TV_PLAYER__
    {0, NULL, mmi_mtv_player_enter_usb_mode_callback, NULL},
#endif
    //{0, NULL, mmi_cbm_usb_delete_confirm_callback, NULL},

#ifdef __VRE30_AM__
    {0, NULL, mmi_am_enter_usb_mode, NULL},
#endif

#ifdef __VRE__
    {0, NULL, mmi_vre_enter_usb_mode, NULL},
#endif

#ifdef __MMI_BROWSER_2__
    {0, NULL, mmi_brw_app_deinit, (ChkFuncPtr)mmi_brw_is_browser_active},
#endif
#ifdef __A8BOX_SUPPORT__
    {0, NULL, mmi_a8box_enter_usb_mode, NULL},
#endif
#ifdef __QQIM_SUPPORT__
    {0, NULL, ExitQQ, NULL},
#endif
#if (defined(__MMI_UNIFIED_COMPOSER__) || defined(__MMI_MMS_STANDALONE_COMPOSER__))
    {0, NULL, mmi_uc_usb_ms_plug_in_hdlr, NULL},
#endif

#ifdef OPERA_V10_BROWSER
    {0, NULL, mmi_opera_brw_enter_usb_mode_hdlr, mmi_opera_brw_usb_mode_check},
#endif


    /* add callback before this */
    {0, NULL, NULL, NULL}
};

static mmi_usb_ms_callback_struct mmi_usb_reinit_callback[] = 
{
    {0, NULL, PhnsetReadWallpaper, NULL},           /* resume wallpaper from secondary flash */
    {0, NULL, PhnsetReadNvramScrSvrSettings, NULL}, /* resume screensaver from secondary flash */
#ifdef __MMI_POWER_ON_OFF_DISPLAY__
    {0, NULL, PhnsetReadPwrOnOffDisp, NULL},        /* resume power on/off disp. from secondary flash */
#endif 
#ifdef __MMI_AUDIO_PLAYER__
	{0, NULL, mmi_audply_usb_plug_out_hdlr, NULL},
#endif /* __MMI_AUDIO_PLAYER__ */ 
#ifdef __MMI_MEDIA_PLAYER__
	{0, NULL, mmi_medply_plst_usb_plug_out_hdlr, NULL},
#endif	/* __MMI_MEDIA_PLAYER__ */
#ifdef __PLST_SERVICE_DB_SUPPORT__
    {0, NULL, srv_plst_usb_plug_out_hdlr, NULL},
#endif
#ifdef __MMI_EMAIL__
    {0, NULL, mmi_email_init_usb, NULL}, /* initialize email module */
#endif 
    {0, NULL, srv_fmgr_drv_init, NULL},
#ifdef MMS_SUPPORT
    {MSG_ID_MMS_EXIT_USB_MODE_RSP, NULL, mms_on_exit_usb_mode, mmi_usb_mms_check_to_reinit},
#endif 
#ifdef __MMI_DOWNLOADABLE_THEMES_SUPPORT__
    {0, NULL, mmi_tm_init_theme_usb_mode_plugout_callback, NULL},
#endif 
#ifdef __MMI_EBOOK_READER__
    {0, NULL, mmi_ebook_usb_mode_off, NULL},
#endif 
#ifdef __MMI_MMS_2__
    {0, NULL, srv_mms_usb_mode_off, NULL},
#endif

#ifdef __MMI_MMS_BGSR_SUPPORT__
    {0, NULL, mmi_mms_bgsr_usb_end_cb, NULL},
#endif /*__MMI_MMS_BGSR_SUPPORT__*/

#ifdef __MMI_MOBILE_TV_PLAYER__
    {0, NULL, mmi_mtv_player_exit_usb_mode_callback, NULL},
#endif

#ifdef __VRE__
    {0, NULL, mmi_vre_exit_usb_mode, NULL},
#endif

#ifdef __A8BOX_SUPPORT__
    {0, NULL, mmi_a8box_exit_usb_mode, NULL},
#endif

#if defined(__MMI_GADGET_SUPPORT__)
    {0, NULL, srv_wgtmgr_exit_usb_mode_hdlr, NULL},
#endif

#if defined(OPERA_V10_BROWSER) || defined(__GADGET_SUPPORT__)
    {0, NULL, npplg_av_main_exit_usb_mode, NULL},
#endif

#ifdef __MMI_VUI_SHELL_IMAGE_VIEWER__
    {0, NULL, vadp_shell_imgviewer_check_filelist, NULL},
#endif

#if defined(__MMI_VCARD__) || defined(__MMI_VCALENDAR__)|| defined(__MMI_VBOOKMARK__)
    {0, NULL, mmi_vobj_usb_mode_plugout_callback, NULL},
#endif

    /* add callback before this */
    {0, NULL, NULL}
};

static U8 counter_of_deinit = 0;
static U8 counter_of_reinit = 0;

#endif /* __USB_IN_NORMAL_MODE__ */ 


/*****************************************************************************
 * FUNCTION
 *  InitializeUsbScr
 * DESCRIPTION
 *  This function is initial function for usb boot mode
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *****************************************************************************/
void InitializeUsbScr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_FRAMEWORK_BACKWARD_COMPATIBLE__
    InitUnicodeSupport();
#endif

    PhnsetReadGPIOSetting();
    
    mmi_gpio_init_context();
    
    InitMMIUsbContext();
    SetUsbStatus(MMI_USBSTATUS_MS);

    MMIUSB_SET_FLAG(MMIUSB_MASK_IS_CONNECT);
    mmi_fe_init();
    InitFramework();
    setup_UI_wrappers();

    PopulateResData();
    initialize_UI_demo();

    /*init sound effect settings, this shall be done before set lang by nvram 
      since it might effect equalizer values*/
    InitSoundEffect();
    PhnsetSetLangByNvramValue(); 

    mdi_audio_init();

#ifdef __NVRAM_IN_USB_MS__
    InitHardwareEvents();
    InitTime();

    srv_prof_init();

#if defined(__MMI_TOUCH_SCREEN__) || defined(__MMI_HANDWRITING_PAD__)
    PhnsetReadNvramCalibrationData();
#endif 

    /* InitSettingNVRAM(); */

    PhnsetRestoreToNvramContrast();

    InitAlarmFrameWork();
    mmi_alm_disable_pwroff();

    StartLEDPatternCharging();
    g_charbat_context.PowerOnCharger = 1;
    wgui_status_icon_bar_set_icon_animate(STATUS_ICON_BATTERY_STRENGTH);
    wgui_status_icon_bar_set_icon_animate(STATUS_ICON_SUBLCD_BATTERY_STRENGTH);
    wgui_status_icon_bar_update();

    PendingAlarmReminder();
#endif /* __NVRAM_IN_USB_MS__ */ 

    InitChargerPwrOn();
    InitUsbModeEventHandler();
    /* Enter USB mode will begin charge itself */
    /* Turn this flag on to prevent backlight off */
    g_charbat_context.isChargerConnected = 1;
    
    ShowUsbScr();    
    TurnOnBacklight(1);
}


/*****************************************************************************
 * FUNCTION
 *  InitMMIUsbContext
 * DESCRIPTION
 *  This function is to initialize the mmi usb context.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *****************************************************************************/
void InitMMIUsbContext(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/


    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (MMIUSB_GET_FLAG(MMIUSB_MASK_BEFORE_POWERON) == MMI_TRUE)
    {
        /* Usb has been pluged in before power on */
        MMI_USB_CTX(usb_status) = MMI_USBSTATUS_PLUGIN;
        MMI_USB_CTX(usb_status_prev) = MMI_USBSTATUS_PLUGOUT;
    }
    else
    {
        /* Usb is not pluged in */
        MMI_USB_CTX(usb_status) = MMI_USBSTATUS_PLUGOUT;
        MMI_USB_CTX(usb_status_prev) = MMI_USBSTATUS_UNKNOW;
    }

    MMI_USB_CTX(ps_port_prev) = uart_port1;
    MMI_USB_CTX(tst_port_prev) = uart_port2;
    MMI_USB_CTX(EndKeyFuncPtr) = NULL;

    mmi_usb_ptr->reset_type = NORMAL_RESET;

#if defined(__DM_LAWMO_SUPPORT__)
    if (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_NORMAL)
    {
        mmi_dmui_lawmo_register_notify(MMI_DMUI_APP_ID_USB,
                                       MMI_DMUI_MO_TYPE_LAWMO_LOCK,
                                       mmi_usb_dmui_lock_notify);
    }
#endif

}


/*****************************************************************************
 * FUNCTION
 *  InitUsbModeEventHandler
 * DESCRIPTION
 *  This function is to initialize the event handler
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: None(?)
 *****************************************************************************/
void InitUsbModeEventHandler(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SetProtocolEventHandler(mmi_frm_key_handle, MSG_ID_MMI_EQ_KEYPAD_DETECT_IND);

    SetProtocolEventHandler(UsbDetectIndHdlr, PRT_EQ_USB_DETECT_IND);
    //SetProtocolEventHandler(UsbGpioDetectIndHdlr, PRT_EQ_GPIO_DETECT_IND);
#ifdef __MMI_USB_COPY_RIGHT__
    SetProtocolEventHandler(UsbPowerOnFinishHdlr, PRT_EQ_USB_POWER_ON_FINISH_IND);
#endif 

    /* SetProtocolEventHandler(UsbConfigRspHdlr, PRT_EQ_USBCONFIG_RSP ); */
}

/* ===========================================================================================  */
/* ===  Query Functions  */
/* ===========================================================================================  */


/*****************************************************************************
 * FUNCTION
 *  GetUsbStatus
 * DESCRIPTION
 *  This function is to get the usb state
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: enum MMI_USB_STATUS(?)
 *****************************************************************************/
MMI_USB_STATUS GetUsbStatus(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CUR_STATE, MMI_USB_CTX(usb_status));

    return MMI_USB_CTX(usb_status);
}


/*****************************************************************************
 * FUNCTION
 *  SetUsbStatus
 * DESCRIPTION
 *  This function is to set the usb state
 *  
 *  PARAMETERS: enum MMI_USB_STATUS
 *  state       [IN]        
 *  RETURNS: void(?)
 *****************************************************************************/
void SetUsbStatus(MMI_USB_STATUS state)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_STATE_TRANSITION, MMI_USB_CTX(usb_status), state);
    MMI_USB_CTX(usb_status_prev) = GetUsbStatus();
    MMI_USB_CTX(usb_status) = state;
}


/*****************************************************************************
 * FUNCTION
 *  GetUsbDefAppSetting
 * DESCRIPTION
 *  This function is to get defined usb port app
 *  
 *  PARAMETERS: enum MMI_USB_STATUS
 *  void
 *  RETURNS: void(?)
 *****************************************************************************/
U8 GetUsbDefAppSetting(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S16 pError;
    U8 app;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ReadValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);

    if (pError != NVRAM_READ_SUCCESS)
    {
        app = USB_SWITCH_PORT_APP_DATA;
    }
    else if (app == 0xFF)
    {
        app = USB_SWITCH_PORT_APP_DATA;
        WriteValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);
    }
    else if (app != USB_SWITCH_PORT_APP_DATA && app != USB_SWITCH_PORT_APP_DEBUG)
    {
        app = USB_SWITCH_PORT_APP_DATA;
        WriteValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);
    }

//    ASSERT((app == USB_SWITCH_PORT_APP_DATA) || (app == USB_SWITCH_PORT_APP_DEBUG));

    return app;
}


/*****************************************************************************
 * FUNCTION
 *  SetUsbBeforePowerOnFlag
 * DESCRIPTION
 *  This function is to set usb plug in/out flag before power on.
 *  (The usb detection indication might come to mmi before power on indication)
 *  
 *  PARAMETERS: void
 *  flag        [IN]        
 *  RETURNS: enum MMI_USB_STATUS(?)
 *****************************************************************************/
void SetUsbBeforePowerOnFlag(BOOL flag)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (flag == MMI_TRUE)
    {
        MMIUSB_SET_FLAG(MMIUSB_MASK_BEFORE_POWERON);
    }
    else
    {
        MMIUSB_RESET_FLAG(MMIUSB_MASK_BEFORE_POWERON);
    }
}


/*****************************************************************************
 * FUNCTION
 *  PermitUsbCfgScreenDisplay
 * DESCRIPTION
 *  This function check the if usb config screen display allowed or not.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: T/F(?)
 *****************************************************************************/
BOOL PermitUsbCfgScreenDisplay(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!srv_bootup_is_completed() || //!mmi_bootup_is_searching_or_idle_reached() || 
/*
#ifndef __MMI_BACKGROUND_CALL__
        srv_ucm_query_call_count(SRV_UCM_CALL_STATE_ALL, SRV_UCM_CALL_TYPE_ALL, NULL) > 0 ||
#else
        (srv_ucm_query_call_count(SRV_UCM_INCOMING_STATE, SRV_UCM_CALL_TYPE_ALL, NULL) > 0) ||
        (srv_ucm_query_call_count(SRV_UCM_OUTGOING_STATE, SRV_UCM_CALL_TYPE_ALL, NULL) > 0)||
#endif      
        IsScreenPresent(mmi_ucm_get_marker_scrn_id()) ||
*/
        //GetActiveScreenId() == SCR_RESTORE_PROCESSING ||    /* During Restore Factory */
        GetActiveScreenId() == POPUP_SCREENID ||
        GetActiveScreenId() == SCR_ID_SAT_MAINMENU ||
        srv_fmgr_async_is_busy() ||
        mmi_phb_check_processing()==MMI_TRUE ||
        gSecuritySetupContext.PINBlocked ||                 /* PUK needed after SIM lock failed or change PIN1 failed */
#ifdef __MMI_DUAL_SIM_MASTER__
        gSecuritySetupContext.PINBlocked_2 ||
#endif    	
    #if defined(__DM_LAWMO_SUPPORT__)
        mmi_dmui_is_phone_lock() ||
        mmi_dmui_get_status(MMI_DMUI_MO_TYPE_LAWMO_LOCK) == MMI_DMUI_STATUS_LAWMO_LOCK_ON_LOCKING ||
    #endif
        srv_shutdown_is_running()) //mmi_shutdown_is_in_power_off_period())
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_SCR_NOT_ALLOW_FALSE);
        return MMI_FALSE;
    }
    else
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_SCR_NOT_ALLOW_TURE);
        return MMI_TRUE;
    }
}

static MMI_BOOL mmi_usb_pre_check_cfg_screen_permit(void)
{
	return PermitUsbCfgScreenDisplay();
}

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display(void)
{
	MMI_BOOL ret;

    //kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_permit_usb_cfg_screen_display");
    
    ret = mmi_frm_nmgr_notify_by_app(MMI_SCENARIO_ID_DEFAULT, MMI_EVENT_NON_TONE, 
        mmi_usb_permit_usb_cfg_screen_display_notify, NULL);

    return ret;
}

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_test(void)
{
	MMI_BOOL ret;

    ret = mmi_frm_nmgr_notify_by_app(MMI_SCENARIO_ID_DEFAULT, MMI_EVENT_NON_TONE, 
        mmi_usb_permit_usb_cfg_screen_display_notify_dummy, NULL);

    return ret;
}

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify(mmi_scenario_id scen_id, void *arg)
{
    MMI_USB_STATUS usb_state;

    usb_state = GetUsbStatus();    
    //kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_permit_usb_cfg_screen_display_notify, state=%d", usb_state);

    if (usb_state == MMI_USBSTATUS_PLUGIN)
    {
    	//kal_prompt_trace(MOD_MMI, "[USB MMI] MMI_USBSTATUS_PLUGIN");
    }
    else if (usb_state == MMI_USBSTATUS_PLUGOUT && MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
    {
        //kal_prompt_trace(MOD_MMI, "[USB MMI] MMI_USBSTATUS_PLUGOUT && MMIUSB_MASK_IS_CONNECT");
    }
#ifdef __USB_IN_NORMAL_MODE__
    else if (mmi_usb_is_leaving_ms_mode())
    {
        //kal_prompt_trace(MOD_MMI, "[USB MMI] mmi_usb_is_leaving_ms_mode");
    }
#endif /* __USB_IN_NORMAL_MODE__ */ 
    else
    {
        //kal_prompt_trace(MOD_MMI, "[USB MMI] Not continue show USB cfg screen");
        return MMI_FALSE;
    }
    
    if (PermitUsbCfgScreenDisplay() == MMI_TRUE)
    {
        if (MMIUSB_GET_FLAG(MMIUSB_MASK_PENDING_TIMER))
        {
            MMIUSB_RESET_FLAG(MMIUSB_MASK_PENDING_TIMER);
            StopTimer(PENDING_USBDETECTION_HDLR_TIMER);
        }

        EntryUsbDetectQueryScr();
        return MMI_TRUE;
    }
    else
    {
        PendingUsbDetectionHdlr();
        return MMI_FALSE;
    }
    
}

static MMI_BOOL mmi_usb_permit_usb_cfg_screen_display_notify_dummy(mmi_scenario_id scen_id, void *arg)
{
    return MMI_TRUE;
}

/* ============================================================================= */
/* ===  Screen Handling Functions  */
/* ============================================================================= */
/*                                                                               */
/*  USB device config screen flow:                                               */
/*                                                                               */
/*  (1) Plug in                                                                  */
/*  ===============                                                              */
/*  == 1. MS     ==                                                              */
/*  == 2. COM    ==                                                              */
/*  ==           ==                                                              */
/*  ==           ==                                                              */
/*  ==           ==                                                              */
/*  ==           ==                                                              */
/*  ===============                                                              */
/*  == OK  = BACK==                                                              */
/*  ===============                                                              */
/*                                                                               */
/*   --> UsbDetectIndHdlr()                                                      */
/*     --> EntryUsbDetectQueryScr()                                              */
/*                                                                               */
/*                                                                               */
/*                                                                               */
/*  (2) Mass storage:                                                            */
/*  ================         ===============          ===============            */
/*  ==            ==         ==           ==          ==           ==            */
/*  == ********** ==         ==  *******  ==          ==     **    ==            */
/*  == * Shut-  * ==         == ********* ==          ==    **     ==            */
/*  == *  Down  * ==         == **+***+** ==          ==   *USB    ==            */
/*  == *  ....  * ==  --->   == ********* ==   --->   ==  ****     ==            */
/*  == *        * ==         == ***&&&*** ==          ==   *MODE   ==            */
/*  == ********** ==         ==  *******  ==          ==   **      ==            */
/*  ==            ==         ==   *****   ==          ==   *  **   ==            */
/*  ================         == Animation ==          ==     **    ==            */
/*  == OK  = BACK ==         ==   .....   ==          ==      **   ==            */
/*  ================         ===============          ===============            */
/* ( Message exchanging      ( Shutdown Ani.)        (Reboot in USB Mode)        */
/* trasition screen 3 sec )                                                      */
/*                                                                               */
/*   -> UsbDetectScrLskHdlr()                           ->InitializeUsbScr()     */
/*    -> UsbConfigFirstPage()                                                    */
/*     -> UsbConfigRspHdlr()                                                     */
/*      -> UsbConfigMassStorage()                                                */
/*                                                                               */
/*                          --> ShutdownSystemOperation()                        */
/*                                                                               */
/*                                                                               */
/*                                                                               */
/*  (2) COM port emulator:                                                       */
/*  ================         ===============                                     */
/*  ==           ===         ==           ==                                     */
/*  == ********** ==         ==  Back     ==                                     */
/*  == * Config * ==         ==       to  ==                                     */
/*  == *  ..... * ==         ==  History  ==                                     */
/*  == *        * ==  --->   ==    Screen ==                                     */
/*  == *        * ==         ==           ==                                     */
/*  == ********** ==         ==           ==                                     */
/*  ==            ==         ==           ==                                     */
/*  ================         ==           ==                                     */
/*  == OK  = BACK ==         ==           ==                                     */
/*  ================         ===============                                     */
/* ( Message exchanging      ( Shutdown Ani.)                                    */
/* trasition screen 3 sec )                                                      */
/*                                                                               */
/*   -> UsbDetectScrLskHdlr()                                                    */
/*    -> UsbConfigFirstPage()                                                    */
/*     -> UsbConfigRspHdlr()                                                     */
/*      -> UsbConfigCDCACM()                                                     */
/*       -> SendUsbGetUartPortToHW()                                             */
/*        -> UsbGetUartPortRspHdlr()                                             */
/*         -> SendUsbUartSwitchToHW()                                            */
/*          -> UsbUartSwitchRspHdlr()                                            */
/*           -> GoBackHistory()                                                  */
/*                                                                               */
/*                                                                               */
/* ============================================================================= */

#ifdef __MMI_SUBLCD__


/*****************************************************************************
 * FUNCTION
 *  ShowSubLCDUsbScr
 * DESCRIPTION
 *  This function is to show sublcd screen for usb boot mode
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED : none(?)
 *****************************************************************************/
void ShowSubLCDUsbScr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ExecSubLCDCurrExitHandler();
    ShowCategory304Screen(IMG_SUB_USB_POWER_ON, 0);
    wgui_status_icon_bar_animate_icon(STATUS_ICON_SUBLCD_BATTERY_STRENGTH);
    SetSubLCDExitHandler(ExitSubLCDUsbScr);
}


/*****************************************************************************
 * FUNCTION
 *  ExitSubLCDUsbScr
 * DESCRIPTION
 *  This function is to exit handler for sublcd screen in usb boot mode
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void ExitSubLCDUsbScr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    SubLCDHistoryNode SubLCDHistory;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SubLCDHistory.entryFuncPtr = ShowSubLCDUsbScr;
    AddSubLCDHistory(&SubLCDHistory);
}


/*****************************************************************************
 * FUNCTION
 *  ShowSubLCDUsbChgrCmpltScr
 * DESCRIPTION
 *  This function is to show sublcd charging complete screen for usb boot mode
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED : none(?)
 *****************************************************************************/
void ShowSubLCDUsbChgrCmpltScr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ExecSubLCDCurrExitHandler();
    ShowCategory304Screen(IMG_USB_SUB_CHGR_CMPLT, 0);
    wgui_status_icon_bar_reset_icon_display(STATUS_ICON_SUBLCD_BATTERY_STRENGTH);
    wgui_status_icon_bar_set_icon_display(STATUS_ICON_SUBLCD_BATTERY_STRENGTH);
    wgui_status_icon_bar_update();
    SetSubLCDExitHandler(ExitSubLCDUsbChgrCmpltScr);
}


/*****************************************************************************
 * FUNCTION
 *  ExitSubLCDUsbChgrCmpltScr
 * DESCRIPTION
 *  This function is to exit handler for sublcd charging complete screen in usb boot mode
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void ExitSubLCDUsbChgrCmpltScr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    SubLCDHistoryNode SubLCDHistory;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    SubLCDHistory.entryFuncPtr = ShowSubLCDUsbChgrCmpltScr;
    AddSubLCDHistory(&SubLCDHistory);
}

#endif /* __MMI_SUBLCD__ */ 


/*****************************************************************************
 * FUNCTION
 *  ShowUsbChgCmpltScr
 * DESCRIPTION
 *  This function is to display main screen for usb boot mode
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: none(?)
 *****************************************************************************/
void ShowUsbChgCmpltScr(void)
{
    mmi_usb_enter_usb_boot_group();
    mmi_usb_show_usb_chg_cmplt_scrn_int();
}

static void mmi_usb_show_usb_chg_cmplt_scrn_int(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
#if defined(__MMI_USB_CHARGER_POWER_ON_SLIM_IMAGE__)
    extern void wgui_cat9002_show(U16 image_icon_1,U16 image_icon_2, U8 *history_buffer, U8   flags);
#endif    

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
    {
        mmi_frm_scrn_close(mmi_usb_get_boot_group(), SCR_USB_CHGR_CMPLT);
        
        if (!mmi_frm_scrn_enter(mmi_usb_get_boot_group(), SCR_USB_CHGR_CMPLT, NULL, mmi_usb_show_usb_chg_cmplt_scrn_int, 0))
        {
            return;
        }
    #if !defined(__MMI_USB_CHARGER_POWER_ON_SLIM_IMAGE__)
        ShowCategory9Screen(0, IMG_USB_CHGR_CMPLT, NULL);
    #else
        wgui_cat9002_show(IMG_ID_USB_POWERON_TEXT,IMG_CHARGER_POWER_DONE_SLIM,NULL,0);
    #endif

    #ifdef __MMI_SUBLCD__
        wgui_status_icon_bar_change_icon_level(STATUS_ICON_SUBLCD_BATTERY_STRENGTH, 100);
        ShowSubLCDScreen(ShowSubLCDUsbChgrCmpltScr);
    #endif /* __MMI_SUBLCD__ */ 

        ClearKeyHandler(KEY_END, KEY_EVENT_DOWN);
        if (mmi_frm_kbd_is_key_supported(KEY_POWER) == MMI_TRUE)
        {
            SetKeyHandler(UsbPwnOnThenPwnOn, KEY_POWER, KEY_LONG_PRESS); 
        }
        else
        {
            SetKeyHandler(UsbPwnOnThenPwnOn, KEY_END, KEY_LONG_PRESS);
        }
    }
    else
    {
        srv_shutdown_exit_system(APP_USBMMI);
    }
        
}


/*****************************************************************************
 * FUNCTION
 *  ShowUsbScr
 * DESCRIPTION
 *  This function is to display main screen for usb boot mode
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: none(?)
 *****************************************************************************/
void ShowUsbScr(void)
{
    mmi_usb_enter_usb_boot_group();
    mmi_frm_scrn_close(mmi_usb_get_boot_group(), SCR_USB_POWER_ON);
    mmi_usb_show_usb_scrn_int();
}

static void mmi_usb_show_usb_scrn_int(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
#if defined(__MMI_USB_CHARGER_POWER_ON_SLIM_IMAGE__)
    extern void wgui_cat9002_show(U16 image_icon_1,U16 image_icon_2, U8 *history_buffer, U8   flags);
#endif

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mmi_usb_ptr->still_image==MMI_USB_POWERON_CHARGING_NONE)
    {
        mmi_usb_ptr->still_image = MMI_USB_POWERON_CHARGING_DISABLE;    /* Still image */
    }
    
    if (!mmi_frm_scrn_enter(mmi_usb_get_boot_group(), SCR_USB_POWER_ON, NULL, mmi_usb_show_usb_scrn_int, 0))
    {
        return;
    }

    mmi_frm_set_curr_scr_blt_mode(MMI_FRM_SCR_BLT_IMMEDIATE);
    
#ifdef __MMI_USB_COPY_RIGHT__

    mmi_popup_display_simple((WCHAR*)GetString(STR_USB_POWER_ON_PROCESSING), MMI_EVENT_PROGRESS, mmi_usb_enter_usb_boot_group(), NULL);
#else 

    #if !defined(__MMI_USB_CHARGER_POWER_ON_SLIM_IMAGE__)
        if (mmi_usb_ptr->still_image==MMI_USB_POWERON_CHARGING_ENABLE)
        {
            ShowCategory106Screen(IMG_ID_USB_CHARGING, NULL, NULL);
        }
        else if (mmi_usb_ptr->still_image==MMI_USB_POWERON_CHARGING_DISABLE)
        {
            ShowCategory106Screen(IMG_USB_POWER_ON, NULL, NULL);            
        }
    #else
        if (mmi_usb_ptr->still_image==MMI_USB_POWERON_CHARGING_ENABLE)
        {
            wgui_cat9002_show(IMG_ID_USB_POWERON_TEXT,IMG_CHARGER_POWER_ON_SLIM,NULL,0);
        }
        else if (mmi_usb_ptr->still_image==MMI_USB_POWERON_CHARGING_DISABLE)
        {
            wgui_cat9002_show(IMG_ID_USB_POWERON_TEXT,0,NULL,0);           
        }    
    #endif
#endif 

#ifdef __MMI_SUBLCD__
    ShowSubLCDScreen(ShowSubLCDUsbScr);
#endif 

    if (mmi_frm_kbd_is_key_supported(KEY_POWER) == MMI_TRUE)
    {
        SetKeyHandler(UsbPwnOnThenPwnOn, KEY_POWER, KEY_LONG_PRESS);        
    }
    else
    {
        SetKeyHandler(UsbPwnOnThenPwnOn, KEY_END, KEY_LONG_PRESS);
    }
}


/*****************************************************************************
 * FUNCTION
 *  EntryUsbDetectQueryScr
 * DESCRIPTION
 *  This function is to display "Usb Config:" screen as received USB_DETECT_IND
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: none(?)
 *****************************************************************************/
void EntryUsbDetectQueryScr(void)
{
    mmi_usb_enter_normal_group();
    mmi_usb_entry_usb_detect_query_scrn_ind();
}

static void mmi_usb_entry_usb_detect_query_scrn_ind(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /* Watch out: must check PermitUsbCfgScreenDisplay first */

    U8 *guiBuffer = NULL;
    U8 *listItemsIcons[8];
    S32 idx;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_DETECT_QUERY_SCR);

   /*************************************************/
    /* Turn on backlight before execute exit handler */
    /* Turn on backlight will blt out the LCD buffer */
    /* If execute exit handler before turn on BL     */
    /* migh see the blue background because turn on  */
    /* BL only blt out layer 0, in idle screen,      */
    /* layer 0 is not wallpaper, wallpaper locate at */
    /* layre 1.                                      */
   /*************************************************/

    TurnOnBacklight(1);
   
    if (!mmi_frm_scrn_enter(GRP_ID_USB_MMI, SCR_DEV_USBDETECT, NULL, mmi_usb_entry_usb_detect_query_scrn_ind, 0))
    {
        return;
    }

    /* EntryNewScreen will clear all key handler */
    guiBuffer = mmi_frm_scrn_get_gui_buf (GRP_ID_USB_MMI, SCR_DEV_USBDETECT);

#if defined(__MMI_MAINLCD_176X220__) || defined(__MMI_MAINLCD_240X320__)        /* 041205 Calvin modified */
    entry_full_screen();
#endif 

    /* Set mmi usb status */
    SetUsbStatus(MMI_USBSTATUS_CONF_PAGE);

#if (defined(__MMI_USB_CDCACM_ONLY__) || defined(__MMI_USB_MS_ONLY__))
    {
        U16 str_title;

    #ifdef __MMI_USB_CDCACM_ONLY__

        #ifdef __USB_COM_PORT_ENABLE__
        /* CDCACM Mode only */
        highlightedItemIndex = MMI_USB_CONFIG_MENU_CDCACM;
        str_title = STR_USB_CONFIG_CDCACM_ONLY;
        #endif

    #else /* __MMI_USB_CDCACM_ONLY__ */ /* __MMI_USB_MS_ONLY__ */

        /* MS Mode only */
        #ifdef __USB_MASS_STORAGE_ENABLE__        
        highlightedItemIndex = MMI_USB_CONFIG_MENU_MS;
        str_title = STR_USB_CONFIG_MS_ONLY;
        #endif

    #endif /* __MMI_USB_CDCACM_ONLY__ */ 

        ShowCategory165Screen(
            STR_GLOBAL_YES,
            IMG_GLOBAL_YES,
            STR_GLOBAL_NO,
            IMG_GLOBAL_NO,
            get_string(str_title),
            mmi_get_event_based_image(MMI_EVENT_QUERY),
            0);

    }

#else /* (defined(__MMI_USB_CDCACM_ONLY__) || defined(__MMI_USB_MS_ONLY__)) */ 

    /* Both Mass Storage and CDCACM mode */
    if (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_NORMAL || 
        srv_bootup_is_exception_mode())
    {
#ifndef MMI_USB_MENU_CUI

        /* If normal power on, display selection menus (MS/COM) */

        RegisterHighlightHandler(UsbDetectScrHlightHdlr);

        memset(listItemsIcons, 0, sizeof(listItemsIcons));
        memset(subMenuDataPtrs, 0, sizeof(subMenuDataPtrs));

        idx = 0;
    #ifdef __USB_MASS_STORAGE_ENABLE__        
        subMenuDataPtrs[idx++] = (PU8) GetString(STR_USB_CONFIG_MS);
    #endif

    #ifdef __MMI_WEBCAM__
        subMenuDataPtrs[idx++] = (PU8) GetString(STR_ID_USB_CONFIG_WEBCAM);
    #endif 

    #ifdef __USB_COM_PORT_ENABLE__
        subMenuDataPtrs[idx++] = (PU8) GetString(STR_USB_CONFIG_CDCACM_DATA);
    #endif

    #ifdef __MMI_PICT_BRIDGE_SUPPORT__
        subMenuDataPtrs[idx++] = (PU8) GetString(STR_USB_CONFIG_PICT_BRIGE);    
    #endif

    #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
        subMenuDataPtrs[idx++] = (PU8) GetString(STR_USB_CONFIG_COMPORT_JAVA);
    #endif

    #ifdef  __MTP_ENABLE__
        subMenuDataPtrs[idx++] = (PU8) GetString(STR_USB_CONFIG_MTP);
    #endif

        wgui_list_menu_enable_access_by_shortcut();
        ShowCategory32Screen(
            (PU8) GetString(STR_USB_CONFIG_TITLE),
            NULL,
            (PU8) GetString(STR_GLOBAL_OK),
            (PU8) GetImage(IMG_GLOBAL_OK),
            (PU8) GetString(STR_GLOBAL_BACK),
            (PU8) GetImage(IMG_GLOBAL_BACK),
            idx,
            subMenuDataPtrs,
            listItemsIcons,
            0,
            0,
            guiBuffer);
#else
        mmi_usb_enter_detect_query_list();
        mmi_frm_scrn_close(GRP_ID_USB_MMI, SCR_DEV_USBDETECT);

#endif
    }
    else
    {
        /* If alarm/charger power on, display MS config confirm */
        ShowCategory165Screen(
            STR_GLOBAL_YES,
            IMG_GLOBAL_YES,
            STR_GLOBAL_NO,
            IMG_GLOBAL_NO,
            get_string(STR_USB_CONFIG_MS_ONLY),
            mmi_get_event_based_image(MMI_EVENT_QUERY),
            0);
    }

#endif /* (defined(__MMI_USB_CDCACM_ONLY__) || defined(__MMI_USB_MS_ONLY__)) */ 

    SetLeftSoftkeyFunction(UsbDetectScrLskHdlr, KEY_EVENT_UP);
    SetRightSoftkeyFunction(UsbDetectScrRskHdlr, KEY_EVENT_UP);

    mmi_frm_scrn_set_leave_proc(mmi_usb_get_normal_group(), SCR_DEV_USBDETECT, mmi_usb_detect_cfg_leave_proc);
    
    SetCenterSoftkeyFunction(UsbDetectScrLskHdlr, KEY_EVENT_UP);
    ChangeCenterSoftkey(0, IMG_GLOBAL_COMMON_CSK);
    
#ifdef __MMI_FM_RADIO__
    mmi_fmrdo_search_all_channels_stop();
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  ExitUsbDetectQueryScr
 * DESCRIPTION
 *  This function is exit handler for  "Usb Config:" screen
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: none(?)
 *****************************************************************************/
void ExitUsbDetectQueryScr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_EXIT_DETECT_QUERY_SCR);
#if defined(__MMI_MAINLCD_176X220__) || defined(__MMI_MAINLCD_240X320__)        /* 041205 Calvin modified */
    leave_full_screen();
#endif 
    /*
     * history currHistory;
     * currHistory.scrnID = SCR_DEV_USBDETECT;
     * currHistory.entryFuncPtr = EntryUsbDetectQueryScr;
     * mmi_ucs2cpy((S8*)currHistory.inputBuffer, (S8*)L"");
     * GetCategory32History (currHistory.guiBuffer);
     * AddHistory (currHistory);
     */

}

/* ============================================================================= */
/* ===  Message Handling  Function  */
/* ============================================================================= */
/*                                                                               */
/*  USB device config message flow:                                              */
/*                                                                               */
/*  (1) Mass storage:                                                            */
/*                                                                               */
/*     MMI                               L4                                      */
/*   ========                          =======                                   */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_USBDETECT_IND   |                                      */
/*      |<--------------------------------|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_USBCONFIG_REQ   |                                      */
/*      |-------------------------------->|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_USBCONFIG_RSP   |                                      */
/*      |<--------------------------------|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_POWER_OFF_REQ   |                                      */
/*      |-------------------------------->|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_POWER_OFF_RSP   |                                      */
/*      |<--------------------------------|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_POWER_ON_IND    |                                      */
/*      |<--------------------------------|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*   ========                          =======                                   */
/*                                                                               */
/*                                                                               */
/*                                                                               */
/*                                                                               */
/*  (2) COM Port emulator:                                                       */
/*                                                                               */
/*     MMI                               L4                                      */
/*   ========                          =======                                   */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_USBDETECT_IND   |                                      */
/*      |<--------------------------------|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_USBCONFIG_REQ   |                                      */
/*      |-------------------------------->|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |   MSG_ID_MMI_EQ_USBCONFIG_RSP   |                                      */
/*      |<--------------------------------|                                      */
/*      |                                 |                                      */
/*  ##########################################################################   */
/*  #   |                                 |    ** Interface change 040927 ** #   */
/*  #   |                                 |    L4C will switch specified app #   */
/*  #   |   MSG_ID_MMI_EQ_GET_UART_REQ    |    to usb port and store the     #   */
/*  #   |-------------------------------->|    previous port. When USB device#   */
/*  #   |                                 |    plug out, will switch back    #   */
/*  #   |                                 |    by L4C itself, so it is not   #   */
/*  #   |   MSG_ID_MMI_EQ_GET_UART_RSP    |    necessary to get UART and     #   */
/*  #   |<--------------------------------|    keep in MMI again.            #   */
/*  #   |                                 |                                  #   */
/*  ##########################################################################   */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      MSG_ID_MMI_EQ_USBUART_SWITCH_PORT_REQ                                    */
/*      |<--------------------------------|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      MSG_ID_MMI_EQ_USBUART_SWITCH_PORT_RSP                                    */
/*      |-------------------------------->|                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*      |                                 |                                      */
/*   ========                          =======                                   */
/*                                                                               */
/*                                                                               */
/*                                                                               */
/* ============================================================================= */


/*****************************************************************************
 * FUNCTION
 *  UsbDetectScrHlightHdlr
 * DESCRIPTION
 *  This function assign the highlight item (usb function) for usb config.
 *  
 *  PARAMETERS: highlight function idx
 *  index       [IN]        
 *  RETURNS: void(?)
 *****************************************************************************/
void UsbDetectScrHlightHdlr(S32 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    highlightedItemIndex = index;
}


/*****************************************************************************
 * FUNCTION
 *  UsbDetectScrENDKeyHdlr
 * DESCRIPTION
 *  This function is END key handler at usb config page.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *****************************************************************************/
void UsbDetectScrENDKeyHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_DETECT_SCR_END_KEY_HDLR);

    if (GetUsbStatus() == MMI_USBSTATUS_CONF_PAGE)
    {
		mmi_idle_display();
    /*  add end */    
    }
    else
    {
        mmi_usb_close_detect_query_list();
    }

    SetUsbStatus(MMI_USBSTATUS_IGNORED);

}


/*****************************************************************************
 * FUNCTION
 *  UsbDetectScrRskHdlr
 * DESCRIPTION
 *  This function is right softkey handler at usb config page.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *****************************************************************************/
void UsbDetectScrRskHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (GetScreenCountInHistory())
    {
        mmi_frm_scrn_close_active_id();
        mmi_usb_close_detect_query_list();
        SetUsbStatus(MMI_USBSTATUS_IGNORED);
    }
    else
    {
        srv_shutdown_exit_system(APP_USBMMI);
    }
}


/*****************************************************************************
 * FUNCTION
 *  UsbDetectScrLskHdlr
 * DESCRIPTION
 *  This function is left softkey handler at usb config page.
 *  (Press left softkey means start to config usb device as certain function)
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: highlightedItemIndex, the usb function index(?)
 *****************************************************************************/
void UsbDetectScrLskHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DSM_NEW__
	if(MR_SUCCESS == mr_app_system_event_hdlr(MR_SYSMSG_ENTER_USB, 0, 0))
		return;
#endif

#if defined(__DM_LAWMO_SUPPORT__)
    if (g_mmi_usb_dmui_lock_process == MMI_TRUE)
    {
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_INVALID), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
        return;
    }
#endif

    switch (highlightedItemIndex)
    {
    #ifdef __USB_MASS_STORAGE_ENABLE__        
        case MMI_USB_CONFIG_MENU_MS:    /* MASS Storage */
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_MS);
            mmi_usb_ptr->usb_ms_storage_owner = MMI_USB_MS_STORAGE_MS;
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_MASS_STORAGE);
            break;
    #endif /* #ifdef __USB_MASS_STORAGE_ENABLE__ */
    #ifdef __MMI_WEBCAM__
        case MMI_USB_CONFIG_MENU_WEBCAM:    /* Webcam */
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_WEBCAM);
            break;
    #endif /* __MMI_WEBCAM__ */ 

    #ifdef __USB_COM_PORT_ENABLE__
        case MMI_USB_CONFIG_MENU_CDCACM:    /* Com port emulator */
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_CDCACM);
            mmi_usb_ptr->usb_comport_owner = MMI_USB_COMPORT_PS;			
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_CDC_ACM);
            break;
    #endif

    #ifdef __MMI_PICT_BRIDGE_SUPPORT__
        case MMI_USB_CONFIG_MENU_PICTBRIGE:
    #ifdef MMI_ON_HARDWARE_P
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_PICTBRIGE);
    #else /* MMI_ON_HARDWARE_P */ 
            mmi_frm_scrn_close_active_id();
    #endif /* MMI_ON_HARDWARE_P */             
            break;
    #endif

    #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
       case MMI_USB_CONFIG_MENU_JAVA_CONNECTION:    /* JAVA CONNECTION */
            mmi_usb_ptr->usb_comport_owner = MMI_USB_COMPORT_JAVA;
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_CDC_ACM);
            break;
    #endif

    #ifdef  __MTP_ENABLE__
       case MMI_USB_CONFIG_MENU_MTP:    /* MTP */
            mmi_usb_ptr->usb_ms_storage_owner = MMI_USB_MS_STORAGE_MTP;
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_MTP);
            break;
    #endif
	
        case MMI_USB_CONFIG_MENU_RESERVED:  /* reserved */
        default:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_NOTHING);
            mmi_frm_scrn_close_active_id();
            break;
    }
}


/*****************************************************************************
 * FUNCTION
 *  PendingUsbDetectionTimeoutHdlr
 * DESCRIPTION
 *  This function try to display usb config screen if usb device still there.
 *  If could not display, will wait a period of time and retry again.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void PendingUsbDetectionTimeoutHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_USB_STATUS usb_state;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_PEND_DETECT_TIMEOUT);

    usb_state = GetUsbStatus();

    if (usb_state == MMI_USBSTATUS_PLUGIN)
    {
    	MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_PENDING_TIMEOUT_HDLR_PLUG_IN);
    	MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_STATE_TRANSITION, MMI_USB_CTX(usb_status_prev), MMI_USB_CTX(usb_status));
    	
        StopTimer(PENDING_USBDETECTION_HDLR_TIMER);

		if (!mmi_usb_pre_check_cfg_screen_permit())
		{
			MMIUSB_SET_FLAG(MMIUSB_MASK_PENDING_TIMER);
			StartTimer(
				PENDING_USBDETECTION_HDLR_TIMER,
				PENDING_USBDETECTION_HDLR_TIMER_DUR,
				PendingUsbDetectionTimeoutHdlr);
		}
		else
		{
			MMIUSB_RESET_FLAG(MMIUSB_MASK_PENDING_TIMER);
			mmi_usb_permit_usb_cfg_screen_display();
        }
    }
    else if (usb_state == MMI_USBSTATUS_PLUGOUT && MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_PENDING_TIMEOUT_HDLR_PLUT_OUT);
    	MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_STATE_TRANSITION, MMI_USB_CTX(usb_status_prev), MMI_USB_CTX(usb_status));

        StopTimer(PENDING_USBDETECTION_HDLR_TIMER);

		if (!mmi_usb_pre_check_cfg_screen_permit())
		{
			MMIUSB_SET_FLAG(MMIUSB_MASK_PENDING_TIMER);
			StartTimer(
				PENDING_USBDETECTION_HDLR_TIMER,
				PENDING_USBDETECTION_HDLR_TIMER_DUR,
				PendingUsbDetectionTimeoutHdlr);
		}
        else
		{
			SetUsbStatus(MMI_USBSTATUS_PLUGIN);
			mmi_usb_permit_usb_cfg_screen_display();
        }
    }
#ifdef __USB_IN_NORMAL_MODE__
    else if (mmi_usb_is_leaving_ms_mode())
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_PENDING_TIMEOUT_HDLR_LEAVING_MS);
    	MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_STATE_TRANSITION, MMI_USB_CTX(usb_status_prev), MMI_USB_CTX(usb_status));

        StopTimer(PENDING_USBDETECTION_HDLR_TIMER);
        StartTimer(
            PENDING_USBDETECTION_HDLR_TIMER,
            PENDING_USBDETECTION_HDLR_TIMER_DUR,
            PendingUsbDetectionTimeoutHdlr);
    }
#endif /* __USB_IN_NORMAL_MODE__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  PendingUsbDetectionHdlr
 * DESCRIPTION
 *  This function is to handle panding usb detection event. When usb detected
 *  at critical time and is not allow to display config screen, the detected event
 *  will be pending. After the time over, shall call this to deal with the pending
 *  event. This will start a timer then show config screen to prevent screen overlap
 *  case by function call.
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void PendingUsbDetectionHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_USB_STATUS usb_state;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_START_TIMER);

    usb_state = GetUsbStatus();

    switch (usb_state)
    {
        case MMI_USBSTATUS_PLUGIN:
    #ifdef __USB_IN_NORMAL_MODE__
        case MMI_USBSTATUS_MS_PROC:
    #endif 
        {
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_START_PEND_TIMEER);

            MMIUSB_SET_FLAG(MMIUSB_MASK_PENDING_TIMER);
            StartTimer(
                PENDING_USBDETECTION_HDLR_TIMER,
                PENDING_USBDETECTION_HDLR_TIMER_DUR,
                PendingUsbDetectionTimeoutHdlr);
            break;
        }
        default:
            break;
    }
}


/*****************************************************************************
 * FUNCTION
 *  UsbGpioDetectIndHdlr
 * DESCRIPTION
 *  This function is usb mode battery status indication handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbGpioDetectIndHdlr(void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_eq_gpio_detect_ind_struct *gpioDetectInd;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    gpioDetectInd = (mmi_eq_gpio_detect_ind_struct*) info;

    switch (gpioDetectInd->gpio_device)
    {

    #ifdef __MMI_CLAMSHELL__

            /* Clam open */
        case EXT_DEV_CLAM_OPEN:
            //gMMIClamState = 1;
            if (srv_bootup_is_booting() == MMI_FALSE)
            {
                /* lcd_backlight = 0; */
                if (AlmIsRTCPwron())
                {
                    TurnOnBacklight(0);
                }
                else
                {
                    TurnOnBacklight(1);
                }
            }
            break;

            /* Clam close */
        case EXT_DEV_CLAM_CLOSE:
            //gMMIClamState = 0;
            if (srv_bootup_is_booting() == MMI_FALSE)
            {
                CloseBacklight();
            }

            break;

    #endif /* __MMI_CLAMSHELL__ */ 

        default:
            break;
    }

    /* Robin 0715: Popup screen display permisstion flag */
    /* Shall reset this flag in the end of msg handler func */
    SetInterruptPopupDisplay(POPUP_ALL_ON);
}

#ifdef __MMI_USB_COPY_RIGHT__


/*****************************************************************************
 * FUNCTION
 *  UsbPowerOnFinishHdlr
 * DESCRIPTION
 *  This function is usb power on finish indication event handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbPowerOnFinishHdlr(void *info)
{
    mmi_usb_enter_usb_boot_group();
    mmi_usb_poweron_finish_scrn_int();
}

static void mmi_usb_poweron_finish_scrn_int(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (!mmi_frm_scrn_enter(mmi_usb_get_boot_group(), SCR_USB_POWER_ON_DONE, NULL, mmi_usb_poweron_finish_scrn_int, 0))
    {
        return;
    }


    ShowCategory106Screen(IMG_USB_POWER_ON, NULL, NULL);

#ifdef __MMI_SUBLCD__
    ShowSubLCDScreen(ShowSubLCDUsbScr);
#endif 
    if (mmi_frm_kbd_is_key_supported(KEY_POWER) == MMI_TRUE)
    {
        SetKeyHandler(UsbPwnOnThenPwnOn, KEY_POWER, KEY_LONG_PRESS);        
    }
    else
    {
        SetKeyHandler(UsbPwnOnThenPwnOn, KEY_END, KEY_LONG_PRESS);
    }
}
#endif /* __MMI_USB_COPY_RIGHT__ */ 


/*****************************************************************************
 * FUNCTION
 *  UsbDetectIndHdlr
 * DESCRIPTION
 *  This function is usb detect indication event handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbDetectIndHdlr(void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_eq_usbdetect_ind_struct *p = (mmi_eq_usbdetect_ind_struct*) info;
    MMI_USB_STATUS usb_state;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_DETECT_SCR);

    usb_state = GetUsbStatus();

    if (p->action == DEVUSB_DETECT_ACTION_PLUGIN)
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_DETECT_SCR_PLUGIN);

        MMIUSB_SET_FLAG(MMIUSB_MASK_IS_CONNECT);
        if (!srv_bootup_is_launched())
        {
            SetUsbBeforePowerOnFlag(MMI_TRUE);
            SetUsbStatus(MMI_USBSTATUS_PLUGIN);
            PendingUsbDetectionHdlr();
        }
        else
        {
            switch (usb_state)
            {
                case MMI_USBSTATUS_PLUGOUT:
                {
                    SetUsbStatus(MMI_USBSTATUS_PLUGIN);

                    if (mmi_usb_permit_usb_cfg_screen_display())
                    {
                    }
                }
                    break;

                case MMI_USBSTATUS_MS:
                    /* USB power on */
                    break;

                case MMI_USBSTATUS_MS_PROC:
                {
                #ifdef __USB_IN_NORMAL_MODE__
                    PendingUsbDetectionHdlr();
                #endif /* __USB_IN_NORMAL_MODE__ */ 
                    break;
                }
                case MMI_USBSTATUS_WEBCAM:
                    break;
                case MMI_USBSTATUS_CDCACM_PROC:
                    /* case MMI_USBSTATUS_CDCACM_SWCHPORT: */
                case MMI_USBSTATUS_UNKNOW:
                case MMI_USBSTATUS_PLUGIN:
                case MMI_USBSTATUS_CONF_PAGE:
                case MMI_USBSTATUS_IGNORED:
                case MMI_USBSTATUS_CDCACM:
                case MMI_USBSTATUS_PICTBRIGE:
                case MMI_USBSTATUS_JAVA_CONNECT:
                default:
                    //kal_prompt_trace(MOD_MMI, "[USB MMI] Plut in Err state(%d)", usb_state);
                    break;
            }
        }
    }   /* p->action == DEVUSB_DETECT_ACTION_PLUGIN */
    else if (p->action == DEVUSB_DETECT_ACTION_PLUGOUT)
    {
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_DETECT_SCR_PLUGOUT);

        StopTimer(PENDING_USBDETECTION_HDLR_TIMER);

        MMIUSB_RESET_FLAG(MMIUSB_MASK_IS_CONNECT);
        if (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_NORMAL &&
            !srv_bootup_is_launched())
        {
            SetUsbBeforePowerOnFlag(MMI_FALSE);
            StopTimer(PENDING_USBDETECTION_HDLR_TIMER);
            SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
        }
        else
        {
            switch (usb_state)
            {
                case MMI_USBSTATUS_CONF_PAGE:
                {
                    /* detect last query screen is in history */
                    mmi_usb_close_detect_query_list();
                    SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
                    break;
                }
            #ifdef __MMI_PICT_BRIDGE_SUPPORT__
                case MMI_USBSTATUS_PICTBRIGE:
                    /* notify pict_brige usb plug-out event */
                    mmi_pict_dps_disconnect_ind();
            #endif /* __MMI_PICT_BRIDGE_SUPPORT__ */
                case MMI_USBSTATUS_CDCACM:
                case MMI_USBSTATUS_CDCACM_PROC:
                case MMI_USBSTATUS_PLUGIN:
                case MMI_USBSTATUS_IGNORED:    
                    /* Show the remove popup, then go on */
                    if (srv_bootup_is_booting() == MMI_FALSE)
                    {
                        {
                            //BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
                        }
                    }
                case MMI_USBSTATUS_PLUGOUT:
                {
                    mmi_usb_close_detect_query_list();
                    SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
                    break;
                }
                case MMI_USBSTATUS_MS_PROC:
                case MMI_USBSTATUS_MS:
                {
                #if defined(__USB_IN_NORMAL_MODE__) || defined(__NVRAM_IN_USB_MS__)
                    mmi_usb_send_removal_event_req();
                #else 
                    mmi_usb_close_detect_query_list();
                #endif 
                    break;
                }
            #ifdef __MMI_WEBCAM__
                case MMI_USBSTATUS_WEBCAM:
                {
                    mmi_usb_close_detect_query_list();
                    mmi_webcam_exit();
                    SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
                    break;
                }
            #endif /* __MMI_WEBCAM__ */ 

            #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
                case MMI_USBSTATUS_JAVA_CONNECT:
                {
                    /* Show the remove popup, then go on */
                    if (srv_bootup_is_booting() == MMI_FALSE)
                    {
                        //BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
                    }

                    jvm_comm_usb_in_java_register_cb(MMI_FALSE);

                    mmi_usb_close_detect_query_list();
                    SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
                    break;
                }					
            #endif				
			
                case MMI_USBSTATUS_UNKNOW:
                default:
                {
                    //kal_prompt_trace(MOD_MMI, "[USB MMI] Plut out Err state(%d)", usb_state);
                    mmi_usb_close_detect_query_list();
                    SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
                    break;
                }
            }
        }
    }
    else
    {
        //kal_prompt_trace(MOD_MMI, "[USB MMI] Err USB detect action(%d)", p->action);
    }

    mmi_usb_send_detect_ack();

}


/*****************************************************************************
 * FUNCTION
 *  UsbConfigFirstPage
 * DESCRIPTION
 *  Display transition screen and send usb config request (MSG_ID_MMI_EQ_USBCONFIG_REQ) to HW
 *  
 *  PARAMETERS: mode, ms or cdcacm
 *  mode        [IN]        
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbConfigFirstPage(U8 mode)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
//#if defined(__MMI_ONLY_ONE_UART_AVAILABLE__)
    U8 ps_port;
    U32 ps_baudrate;
//#endif

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_CONF_FIRST_PAGE, mode);

    if (GetUsbStatus() == MMI_USBSTATUS_PLUGOUT)
    {
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
        mmi_usb_close_detect_query_list();
        return;
    }

    if (srv_ucm_query_call_count(SRV_UCM_CALL_STATE_ALL, SRV_UCM_CALL_TYPE_ALL, NULL) > 0 &&
        (mode == DEVUSB_CFG_ACTION_MASS_STORAGE
    #ifdef __MTP_ENABLE__
        || mode == DEVUSB_CFG_ACTION_MTP
    #endif
    #ifdef __MMI_WEBCAM__
        || mode == DEVUSB_CFG_ACTION_WEBCAM
    #endif
        ))
    {
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_CURRENTLY_NOT_AVAILABLE_IN_CALL), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
        mmi_usb_close_detect_query_list();
        return;
    }

#ifdef __MMI_IRDA_SUPPORT__
    else if (mode == DEVUSB_CFG_ACTION_CDC_ACM && mmi_irda_is_ircomm_active() == MMI_TRUE)
    {
        /* currently, when IrComm is active, switch port not allow! */
        mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_IRCOMM_OCCUPY_CDCACM), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);

        mmi_usb_close_detect_query_list();
        return;
    }
#endif /* __MMI_IRDA_SUPPORT__ */ 

#ifdef __MMI_WEBCAM__
    if (mode == DEVUSB_CFG_ACTION_WEBCAM)
    {
        mmi_webcam_enter();
        mmi_usb_close_detect_query_list();
        return;
    }
#endif /* __MMI_WEBCAM__ */ 

//#if defined(__MMI_ONLY_ONE_UART_AVAILABLE__)
    if (mode == DEVUSB_CFG_ACTION_CDC_ACM)
    {
        nvram_get_ps(&ps_port, &ps_baudrate);

        if (ps_port == uart_port_null)
        {
            mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_UART2_OCCUPIED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
            mmi_usb_close_detect_query_list();
            return;
        }
    }
//#endif /* __MMI_ONLY_ONE_UART_AVAILABLE__ */

    mmi_frm_display_dummy_screen_ex(mmi_usb_get_normal_group(), GLOBAL_SCR_DUMMY);
    mmi_usb_close_detect_query_list();

    ClearKeyEvents();
    ClearInputEventHandler(MMI_DEVICE_KEY);
    ClearKeyHandler(KEY_END, KEY_EVENT_UP);
    ClearKeyHandler(KEY_END, KEY_EVENT_DOWN);
    ClearKeyHandler(KEY_END, KEY_LONG_PRESS);

    if (mode == DEVUSB_CFG_ACTION_MASS_STORAGE 
        #ifdef __MTP_ENABLE__
        || mode == DEVUSB_CFG_ACTION_MTP
        #endif
        )
    {
    #ifdef __USB_IN_NORMAL_MODE__
        if (!mmi_usb_need_to_reboot())
        {

            mmi_usb_entry_progress_screen();

        #ifdef WAP_SUPPORT
            if (mmi_usb_is_network_services_available() == MMI_TRUE)
            {
                if (!wap_is_ready())
                {
                    mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_SYS_NOT_READY), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);

                    mmi_usb_close_progress_screen();
                    SetUsbStatus(MMI_USBSTATUS_IGNORED);

                    return;
                }
            }
        #endif

        #ifdef MMS_SUPPORT
            if (mmi_usb_is_network_services_available() == MMI_TRUE)
            {
                if (!mma_is_ready_for_usb())
                {
                    mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_SYS_NOT_READY), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
                    
                    mmi_usb_close_progress_screen();
                    SetUsbStatus(MMI_USBSTATUS_IGNORED);

                    return;
                }
            }
        #endif /* MMS_SUPPORT */

            mmi_usb_is_exported = mmi_usb_is_any_exported_drive();
        #if defined(__MSDC_NOT_SUPPORT_HOT_PLUG__) || defined(__MSDC_TFLASH_DAT3_1BIT_HOT_PLUG__)
            if (!mmi_usb_is_exported)
            {
                mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_NO_MS_FOR_PC), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);

                mmi_usb_close_progress_screen();
                SetUsbStatus(MMI_USBSTATUS_IGNORED);
            }
            else
        #endif /* defined(__MSDC_NOT_SUPPORT_HOT_PLUG__) || defined(__MSDC_TFLASH_DAT3_1BIT_HOT_PLUG__) */ 
            {
                SetUsbStatus(MMI_USBSTATUS_MS_PROC);
                mmi_usb_hide_file_related_menus();
                mmi_usb_app_deinit(NULL);
            }
        }
        else
    #endif /* __USB_IN_NORMAL_MODE__ */ 
        {
            //mmi_display_popup((UI_string_type)GetString(STR_USB_SHUTDOWN_SCR), MMI_EVENT_PROGRESS);
            ShowCategory63Screen(
                (PU8) GetString(STR_USB_SHUTDOWN_SCR),
                mmi_get_event_based_image(MMI_EVENT_PROGRESS),
                NULL);  
            if ((srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_USB) || 
                (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_ALARM) ||
                (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_CHARGE))
            {
                UsbConfigMassStorage();
                SetUsbStatus(MMI_USBSTATUS_MS_PROC);
            }
            else
            {
                SendUsbConfigToHW(DEVUSB_CFG_ACTION_MASS_STORAGE);
                SetUsbStatus(MMI_USBSTATUS_MS_PROC);
            }
        }
    }
    else if (mode == DEVUSB_CFG_ACTION_CDC_ACM)
    {
    #if defined(__MMI_ENGINEER_MODE__) && defined(__DSPIRDBG__) && defined(__SWDBG_SUPPORT__)
        S16 pError;
        U8 mode;
    #endif /* defined(__MMI_ENGINEER_MODE__) && defined(__DSPIRDBG__) */ 

        /* avoid entry UART setting screen */
    #ifdef __MMI_ENGINEER_MODE__
        DeleteScreenIfPresent(EM_DEV_INLINE_CONFIG_UART_MENU_SCR);
        DeleteScreenIfPresent(EM_DEV_SET_UART_MENU_SCR);
    #endif /* __MMI_ENGINEER_MODE__ */ 

        SetUsbStatus(MMI_USBSTATUS_CDCACM_PROC);
        mmi_popup_display_simple((WCHAR*)GetString(STR_USB_CDCACM_SETTING_SCR), MMI_EVENT_PROGRESS, mmi_usb_get_normal_group(), NULL);

    #ifdef __MMI_USB_MULTIPLE_COMPORT_SUPPORT__
        #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
            if (mmi_usb_ptr->usb_comport_owner == MMI_USB_COMPORT_JAVA)
            {
                SendUsbConfigToHW(DEVUSB_CFG_ACTION_CDC_ACM);
            }
            else
        #endif
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_MULTI_COM);
    #else
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_CDC_ACM);
    #endif
    }
#ifdef __MMI_PICT_BRIDGE_SUPPORT__
    else if (mode == DEVUSB_CFG_ACTION_PICTBRIGE)
    {
        SetUsbStatus(MMI_USBSTATUS_PICTBRIGE);
        mmi_popup_display_simple((WCHAR*)GetString(STR_USB_PICT_BRIGE_CONFIGING), MMI_EVENT_PROGRESS, mmi_usb_get_normal_group(), NULL);
        SendUsbConfigToHW(DEVUSB_CFG_ACTION_PICTBRIGE);
    }
#endif /* __MMI_PICT_BRIDGE_SUPPORT__ */
    else
    {
        ASSERT(0);
    }
}


/*****************************************************************************
 * FUNCTION
 *  UsbConfigRspHdlr
 * DESCRIPTION
 *  This is usb config response handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbConfigRspHdlr(void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    mmi_eq_usbconfig_rsp_struct *p_usbcfg_struct = (mmi_eq_usbconfig_rsp_struct*) info;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_CONF_RSP_HDLR);
#ifdef __USB_IN_NORMAL_MODE__
    if (p_usbcfg_struct->result != KAL_TRUE)
    {
    #ifdef __NVRAM_IN_USB_MS__
        if (p_usbcfg_struct->mode == DEVUSB_CFG_ACTION_STOP_MS
        #ifdef __MTP_ENABLE__
        || p_usbcfg_struct->mode == DEVUSB_CFG_ACTION_STOP_MTP
        #endif       
        ) 
        {
            ;   /* no matter success or not, must reboot. */
        }
        else
    #endif /* __NVRAM_IN_USB_MS__ */ 
        {
            /* Config error or USB device pluged out, drv will be reset so stop further processing */
            if (MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
            {
                mmi_usb_ptr->ms_config_result = MMI_FALSE;

                /* Tricky, should add a temp state, e.g. MMI_USBSTATUS_CFG_RSP */
                SetUsbStatus(MMI_USBSTATUS_MS);
                mmi_usb_send_removal_event_req();           
                  
                mmi_usb_close_progress_screen();           
                mmi_usb_close_detect_query_list();
            }
            else
            {
                if (p_usbcfg_struct->mode == DEVUSB_CFG_ACTION_MASS_STORAGE 
                    #ifdef __MTP_ENABLE__
                    || p_usbcfg_struct->mode == DEVUSB_CFG_ACTION_MTP
                    #endif
                    )
                {
                    /* Tricky, should add a temp state, e.g. MMI_USBSTATUS_CFG_RSP */
                    SetUsbStatus(MMI_USBSTATUS_MS);
                    mmi_usb_send_removal_event_req();
                }
                else
                {
                    SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
                    mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);

                    mmi_usb_close_progress_screen();
                    mmi_usb_close_detect_query_list();
                }
            }

            return;
        }
    }
#else /* __USB_IN_NORMAL_MODE__ */ 
    if (p_usbcfg_struct->result != KAL_TRUE || GetUsbStatus() == MMI_USBSTATUS_PLUGOUT)
    {

    #ifdef __NVRAM_IN_USB_MS__
        if (p_usbcfg_struct->mode == DEVUSB_CFG_ACTION_STOP_MS
        #ifdef __MTP_ENABLE__
        || p_usbcfg_struct->mode == DEVUSB_CFG_ACTION_STOP_MTP
        #endif
        ) 
        {
            ASSERT(0);   /* no matter success or not, must reboot. */
        }
        else
    #endif /* __NVRAM_IN_USB_MS__ */ 
        {
            if (!MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
            {
                SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
            }

            /* Config error or USB device pluged out, drv will be reset so stop further processing */
            mmi_frm_scrn_close_active_id();

            mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);

            return;
        }
    }
#endif /* __USB_IN_NORMAL_MODE__ */ 

    switch (p_usbcfg_struct->mode)
    {
        case DEVUSB_CFG_ACTION_MASS_STORAGE /* USB_MASS_STORAGE */ :
    #ifdef __MTP_ENABLE__			
        case DEVUSB_CFG_ACTION_MTP:
    #endif
    #ifdef __USB_IN_NORMAL_MODE__
        #if defined(__DM_LAWMO_SUPPORT__)       	
       	    if (g_mmi_usb_dmui_lock_process==MMI_TRUE)
       	    {
       	        /* Exit MS mode, because DM notify comes after deinit done,
       	           so exit here*/
           	    if (mmi_usb_ptr->usb_ms_storage_owner == MMI_USB_MS_STORAGE_MTP)
                {
               #ifdef __MTP_ENABLE__
                    SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MTP);
               #endif
                    break;
                }
                else
                {
            	      SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MS); 	        
                    break;
           	    }
       	    }
        #endif
        
            if (!mmi_usb_need_to_reboot())
            {
                SetUsbStatus(MMI_USBSTATUS_MS);

        #ifdef __MMI_BACKGROUND_CALL__
                if ((srv_ucm_query_call_count(SRV_UCM_INCOMING_STATE, SRV_UCM_CALL_TYPE_ALL, NULL) > 0) ||
                    (srv_ucm_query_call_count(SRV_UCM_OUTGOING_STATE, SRV_UCM_CALL_TYPE_ALL, NULL) > 0))
                {
                    U16 marker_screen = 0, start_screen = 0, end_screen = 0;

                    marker_screen = mmi_ucm_get_marker_scrn_id();
                    GetPreviousScrnIdOf(marker_screen, &end_screen);
                    GetNextScrnIdOf(IDLE_SCREEN_ID, &start_screen);
                    DeleteBetweenScreen(start_screen, end_screen);
                    mmi_usb_close_progress_screen();
                }
                else
                {
                    mmi_idle_display();
                }
        #else
                if (!srv_ucm_query_call_count(SRV_UCM_CALL_STATE_ALL, SRV_UCM_CALL_TYPE_ALL, NULL) > 0)
                {
                    mmi_idle_display();
                }
                else
                {
                    U16 marker_screen = 0, start_screen = 0, end_screen = 0;

                    marker_screen = mmi_ucm_get_marker_scrn_id();

                    GetPreviousScrnIdOf(marker_screen, &end_screen);
                    GetNextScrnIdOf(IDLE_SCREEN_ID, &start_screen);
                    DeleteBetweenScreen(start_screen, end_screen);

//                    DeleteBetweenScreen(CM_SCR_MARKER, IDLE_SCREEN_ID);
                    mmi_usb_close_progress_screen();
                }
         #endif /* #ifndef __MMI_BACKGROUND_CALL__ */
         
                if (mmi_usb_is_exported)
                {
                    mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS, mmi_usb_get_normal_group(), NULL);
                    mmi_usb_close_progress_screen();
                }
                else
                {
                    mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_NO_MS_FOR_PC), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
                }

            }
            else
    #endif /* __USB_IN_NORMAL_MODE__ */ 
            {
                UsbConfigMassStorage();
            }
            break;

        case DEVUSB_CFG_ACTION_CDC_ACM /* USB_CDC_ACM */ :
    #ifdef __MMI_USB_MULTIPLE_COMPORT_SUPPORT__
        case DEVUSB_CFG_ACTION_MULTI_COM:
    #endif
            if (mmi_usb_ptr->usb_comport_owner == MMI_USB_COMPORT_PS)
            {
                UsbConfigCDCACM();
            }
        #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
            else if (mmi_usb_ptr->usb_comport_owner == MMI_USB_COMPORT_JAVA)
            {
                mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS, mmi_usb_get_normal_group(), NULL);
                mmi_usb_close_detect_query_list();
            
                jvm_comm_usb_in_java_register_cb(MMI_TRUE);

                SetUsbStatus(MMI_USBSTATUS_JAVA_CONNECT);
				
            }
        #endif
            else
            {
                ASSERT(0);
            }				
            break;

        case DEVUSB_CFG_ACTION_WEBCAM /* USB_WEBCAM */ :
            /* Do nothing */
            break;

        #if defined(__NVRAM_IN_USB_MS__)
        case DEVUSB_CFG_ACTION_STOP_MS /* USB_STOP_MS */ :
    #ifdef __MTP_ENABLE__
	 case DEVUSB_CFG_ACTION_STOP_MTP /* USB_STOP_MTP */ :
    #endif			
        {
        #if defined(__DM_LAWMO_SUPPORT__)       	
       	    if (g_mmi_usb_dmui_lock_process==MMI_TRUE)
       	    {
            	  /*deinit app */
                mmi_usb_send_removal_event_req();       	        
       	    }
       	    else
        #endif
       	    {
                /* g_pwr_context.PowerOnMode == POWER_ON_USB */
                mmi_usb_send_power_on_req();
            }
            break;
        }
        #endif /* defined(__NVRAM_IN_USB_MS__) */ 

        #ifdef __MMI_PICT_BRIDGE_SUPPORT__
        case DEVUSB_CFG_ACTION_PICTBRIGE: /* PictBrige */
            mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS, mmi_usb_get_normal_group(), NULL);
            mmi_usb_close_detect_query_list();
            break;
        #endif /* __MMI_PICT_BRIDGE_SUPPORT__ */
        
        default:
            mmi_frm_scrn_close_active_id();
            break;
    }
}


/*****************************************************************************
 * FUNCTION
 *  UsbCfgMSTimeoutHdlr
 * DESCRIPTION
 *  This fucntion is mass storage following action after usb conigure response comes,
 *  (MSG_ID_MMI_EQ_USBCONFIG_RSP) and the screen timer expire
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbCfgMSTimeoutHdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (srv_bootup_get_booting_mode() != SRV_BOOTUP_MODE_NORMAL && 
        !srv_bootup_is_exception_mode())
    {
        AlmDeInit();
        SetInterruptPopupDisplay(POPUP_ALL_OFF);
        UsbSendMsgToHW(PRT_POWER_OFF_REQ, NULL, NULL);
    }
    else
    {
        srv_shutdown_normal_start(APP_USBMMI);
    }
}


/*****************************************************************************
 * FUNCTION
 *  UsbConfigMassStorage
 * DESCRIPTION
 *  This fucntion is mass storage following action after usb conigure response comes,
 *  (MSG_ID_MMI_EQ_USBCONFIG_RSP)
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbConfigMassStorage(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_CONF_MS);

    StartTimer(USB_SHUTDOWN_SCR_TIMER, USB_SHUTDOWN_SCR_TIMER_DUR, UsbCfgMSTimeoutHdlr);
}


/*****************************************************************************
 * FUNCTION
 *  UsbConfigCDCACM
 * DESCRIPTION
 *  This fucntion is cdcacm following action after usb conigure response comes,
 *  (MSG_ID_MMI_EQ_USBCONFIG_RSP)
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbConfigCDCACM(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
   /*******************************/
   /** Interface change 040927   **/
   /** See the description above **/
   /*******************************/
    S16 pError;
    U8 app;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_CONF_CDCACM);
    /*   
     * SetProtocolEventHandler (UsbGetUartPortRspHdlr, MSG_ID_MMI_EQ_GET_UART_RSP);
     * SendUsbGetUartPortToHW();
     */

    ReadValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);

    if (pError != NVRAM_READ_SUCCESS)
    {
        app = USB_SWITCH_PORT_APP_DATA;
    }
    else if (app == 0xFF)
    {
        app = USB_SWITCH_PORT_APP_DATA;
        WriteValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);
    }
    else if (app != USB_SWITCH_PORT_APP_DATA && app != USB_SWITCH_PORT_APP_DEBUG)
    {
        app = USB_SWITCH_PORT_APP_DATA;
        WriteValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);
    }

    //ASSERT((app == USB_SWITCH_PORT_APP_DATA) || (app == USB_SWITCH_PORT_APP_DEBUG));

    SendUsbUartSwitchToHW(app, uart_port_usb);
}


/*****************************************************************************
 * FUNCTION
 *  UsbUartSwitchRspHdlr
 * DESCRIPTION
 *  This is uart switch port response handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbUartSwitchRspHdlr(void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_eq_usbuart_switch_port_rsp_struct *rsp_p = info;
    MMI_USB_STATUS usb_state;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_UART_SWITCH_RSP_HDLE);

    usb_state = GetUsbStatus();

    /* if(  usb_state != MMI_USBSTATUS_PLUGOUT )
       GoBackHistory(); */

    if (!(rsp_p->result))
    {
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
    }
    else
    {
        if (usb_state != MMI_USBSTATUS_PLUGOUT)
        {
            /* Finish CDCACM config and port switch, change state */
            SetUsbStatus(MMI_USBSTATUS_CDCACM);
        }
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_DONE), MMI_EVENT_SUCCESS, mmi_usb_get_normal_group(), NULL);
        mmi_usb_close_detect_query_list();

      /*******************************/
      /** Interface change 040927   **/
      /** See the description above **/
      /*******************************/
    #if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #endif /* 0 */ 

    }
}


/*****************************************************************************
 * FUNCTION
 *  UsbGetUartPortRspHdlr
 * DESCRIPTION
 *  This is uart get port response handler
 *  
 *  PARAMETERS: void
 *  info        [?]     
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbGetUartPortRspHdlr(void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_eq_get_uart_rsp_struct *rsp_p = info;
    S16 pError;
    U8 app;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_FUNC_GET_UART_PORT_RSP_HDLE);

    /* Error case, stop subsequence action and return */
    if (GetUsbStatus() == MMI_USBSTATUS_PLUGOUT || rsp_p->result != MMI_TRUE)
    {
        mmi_frm_scrn_close_active_id();
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);

        return;
    }

    /* Get uart port successfully, store in context */
    MMI_USB_CTX(ps_port_prev) = rsp_p->ps_uart_port;
    MMI_USB_CTX(tst_port_prev) = rsp_p->tst_uart_port_ps;

    /* Get the target application to switch COM port */
    /* (set from engineer mode, ps(data) or tst(debug) ) */
    ReadValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);
    if (pError != NVRAM_READ_SUCCESS)
    {
        app = USB_SWITCH_PORT_APP_DATA;
    }
    else if (app == 0xFF)
    {
        app = USB_SWITCH_PORT_APP_DATA;
        WriteValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);
    }
    else if (app != USB_SWITCH_PORT_APP_DATA && app != USB_SWITCH_PORT_APP_DEBUG)
    {
        app = USB_SWITCH_PORT_APP_DATA;
        WriteValue(NVRAM_DEFAULT_USB_APP, &app, DS_BYTE, &pError);
    }

    //ASSERT((app == USB_SWITCH_PORT_APP_DATA) || (app == USB_SWITCH_PORT_APP_DEBUG));

    if (((app == USB_SWITCH_PORT_APP_DATA) && (MMI_USB_CTX(tst_port_prev) == uart_port_usb))
        || ((app == USB_SWITCH_PORT_APP_DEBUG) && (MMI_USB_CTX(ps_port_prev) == uart_port_usb)))
    {
        /* Port confict with other application, other application has already set usb as commulation port */
        mmi_frm_scrn_close_active_id();
        
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);

        return;
    }
    else
    {
        /* Switch the uart port of defined application to usb port */
        /* SetUsbStatus( MMI_USBSTATUS_CDCACM_SWCHPORT ); */
        MMIUSB_SET_FLAG(MMIUSB_MASK_PORT_SWITCHING);
        SendUsbUartSwitchToHW(app, uart_port_usb);
    }

}

/* ===========================================================================================  */
/* ===  Send Message to Protocol Stack  */
/* ===========================================================================================  */


/*****************************************************************************
 * FUNCTION
 *  SendUsbConfigToHW
 * DESCRIPTION
 *  This function is to send usb config request to PS
 *  
 *  PARAMETERS: mode, ms or cdcacm
 *  mode        [IN]        
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void SendUsbConfigToHW(U8 mode)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    mmi_eq_usbconfig_req_struct *p_param;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    p_param = OslConstructDataPtr(sizeof(mmi_eq_usbconfig_req_struct));
    p_param->mode = mode;
    p_param->reserved = 0;

    SetProtocolEventHandler(UsbConfigRspHdlr, MSG_ID_MMI_EQ_USBCONFIG_RSP);
    UsbSendMsgToHW(MSG_ID_MMI_EQ_USBCONFIG_REQ, (void*)p_param, (void*)NULL);

}


/*****************************************************************************
 * FUNCTION
 *  SendUsbUartSwitchToHW
 * DESCRIPTION
 *  This function is to send switch port request to PS
 *  
 *  PARAMETERS:
 *  app             [IN]        data or tst
 *  new_port        [IN]        uart 1, uart 2, usb, ircomm...etc
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void SendUsbUartSwitchToHW(U8 app, U8 new_port)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    mmi_eq_usbuart_switch_port_req_struct *p_param;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_SEND_SWITCH_PORT, app, new_port);

    p_param = OslConstructDataPtr(sizeof(mmi_eq_usbuart_switch_port_req_struct));

   /*******************************/
   /** Interface change 040927   **/
   /** See the description above **/
   /*******************************/
    /* p_param->new_port = new_port; */
    p_param->app = app;

    SetProtocolEventHandler(UsbUartSwitchRspHdlr, PRT_EQ_USBUART_SWITCH_PORT_RSP);
//#ifdef __MMI_DUAL_SIM_SINGLE_CALL__    
//    SetSlaveProtocolEventHandler(UsbUartSwitchRspHdlr, PRT_EQ_USBUART_SWITCH_PORT_RSP);
//#endif
    UsbSendMsgToHW(PRT_EQ_USBUART_SWITCH_PORT_REQ, (void*)p_param, (void*)NULL);

}


/*****************************************************************************
 * FUNCTION
 *  SendUsbGetUartPortToHW
 * DESCRIPTION
 *  This function is to send usb get port request to PS
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void SendUsbGetUartPortToHW(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    UsbSendMsgToHW(MSG_ID_MMI_EQ_GET_UART_REQ, (void*)NULL, (void*)NULL);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_send_detect_ack
 * DESCRIPTION
 *  This function is to send usb detect ack to PS
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
static void mmi_usb_send_detect_ack(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_eq_usbdetect_res_req_struct *param_ack;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    param_ack = OslConstructDataPtr(sizeof(mmi_eq_usbdetect_res_req_struct));

    if (MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
    {
        param_ack->action = DEVUSB_DETECT_ACTION_PLUGIN;
    }
    else
    {
        param_ack->action = DEVUSB_DETECT_ACTION_PLUGOUT;
    }

    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_PENDING_SEND_ACK_TO_PS, param_ack->action);

    UsbSendMsgToHW(MSG_ID_MMI_EQ_USBDETECT_RES_REQ, (void*)param_ack, (void*)NULL);
}


/*****************************************************************************
 * FUNCTION
 *  UsbSendMsgToHW
 * DESCRIPTION
 *  This function is to send message to PS
 *  
 *  PARAMETERS: void
 *  msg_id              [IN]        
 *  local_param_ptr     [?]         
 *  peer_buf_ptr        [?]         
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbSendMsgToHW(U16 msg_id, void *local_param_ptr, void *peer_buf_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 source_id = MOD_L4C;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_SINGLE_CALL__    
    if (msg_id == PRT_EQ_USBUART_SWITCH_PORT_REQ)
    {
        U8 buffer[NVRAM_EF_SYS_CACHE_OCTET_SIZE];
        S16 pError;
    
        ReadRecord(NVRAM_EF_SYS_CACHE_OCTET_LID,
                   NVRAM_SYS_FLIGHTMODE_STATE, 
                   (void*)buffer, 
                   NVRAM_EF_SYS_CACHE_OCTET_SIZE, &pError);
    
        if (buffer[L4C_SETTING_DUAL_SIM_UART] ==  L4C_UART_TO_SIM2)
        {
            source_id = MOD_L4C_2;
        }
    }
#endif /* #ifdef __MMI_DUAL_SIM_SINGLE_CALL__*/    
      
    mmi_frm_send_ilm((oslModuleType) source_id, msg_id, 
                        (oslParaType*)local_param_ptr, (oslPeerBuffType*)peer_buf_ptr);

}


/*****************************************************************************
 * FUNCTION
 *  UsbPwnOnThenPwnOn
 * DESCRIPTION
 *  This function is to send power on request to PS
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS: void(?)
 *  GLOBALS AFFECTED: none(?)
 *****************************************************************************/
void UsbPwnOnThenPwnOn(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_PWRON_THEN_PWRON);

#if defined(__MMI_SUBLCD__)
    lcd_power_on(SUB_LCD, 0);
#endif 

    LCDBackLightOff();

    if (mmi_frm_kbd_is_key_supported(KEY_POWER) == MMI_TRUE)
    {
        ClearKeyHandler(KEY_POWER, KEY_LONG_PRESS);
    }
    else
    {
        ClearKeyHandler(KEY_END, KEY_EVENT_DOWN);
        ClearKeyHandler(KEY_END, KEY_LONG_PRESS);
        ClearKeyHandler(KEY_END, KEY_REPEAT);  
    }      

#if defined(__NVRAM_IN_USB_MS__)
    if (GetUsbStatus() == MMI_USBSTATUS_MS)
    {
	if (USB_Boot_With_MS()==KAL_FALSE)
       {
       #ifdef __MTP_ENABLE__
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MTP);
	#endif
        }
        else
    	{
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MS);
        }
        SetUsbStatus(MMI_USBSTATUS_MS_PROC);
    }
    else
#endif /* defined(__NVRAM_IN_USB_MS__) */ 
    mmi_usb_send_power_on_req();
    mmi_usb_close_usb_boot_group();
}

MMI_BOOL mmi_usb_is_connected(void)
{
    return MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT);
}

#ifdef __USB_IN_NORMAL_MODE__


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_hide_file_related_menus
 * DESCRIPTION
 *  Hide file related menus before enter USB mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_hide_file_related_menus(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* wgui_status_icon_bar_reset_icon_display(STATUS_ICON_USB); */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_restore_file_related_menus
 * DESCRIPTION
 *  Restore file related menus before leave USB mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_restore_file_related_menus(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* wgui_status_icon_bar_set_icon_display(STATUS_ICON_USB); */
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_need_to_reboot
 * DESCRIPTION
 *  Check if it it necessary to reboot for USB mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static MMI_BOOL mmi_usb_need_to_reboot(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

#ifdef __USB_IN_NORMAL_MODE_IMPROVE__
    if (AlmIsRTCPwron())
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }    
#elif !defined(__FS_CARD_SUPPORT__) && !defined(NAND_SUPPORT)
    return MMI_TRUE;
#elif !defined(__FS_CARD_SUPPORT__) && defined(__NOR_NOT_PRESENT__)
    return MMI_TRUE;
#else 
    if (AlmIsRTCPwron())
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }
#endif 
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_progress_del_callback
 * DESCRIPTION
 *  Callback function of delete bmi screen
 * PARAMETERS
 *  p       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
static U8 mmi_usb_progress_del_callback(void *p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_USB_STATUS usb_state = GetUsbStatus();

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (usb_state == MMI_USBSTATUS_MS_PROC)
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_entry_progress_screen
 * DESCRIPTION
 *  USB progress screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_entry_progress_screen(void)
{
    mmi_usb_enter_normal_group();
    mmi_usb_entry_progress_screen_int();
}

static void mmi_usb_entry_progress_screen_int(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_frm_scrn_close(GRP_ID_USB_MMI, SCR_ID_USB_PROGRESS);

    if (!mmi_frm_scrn_enter(GRP_ID_USB_MMI, SCR_ID_USB_PROGRESS, NULL, mmi_usb_entry_progress_screen_int, 0))
    {
        return;
    }
    ShowCategory66Screen(
        STR_USB_CONFIG_TITLE,
        0,
        0,
        0,
        0,
        0,
        (PU8) GetString(STR_GLOBAL_PLEASE_WAIT),
        mmi_get_event_based_image(MMI_EVENT_PROGRESS),
        NULL);

    mmi_frm_scrn_set_leave_proc(GRP_ID_USB_MMI, SCR_ID_USB_PROGRESS, mmi_usb_progress_leave_proc);
}

static mmi_ret mmi_usb_progress_leave_proc(mmi_event_struct *param)
{
    MMI_USB_STATUS usb_state = GetUsbStatus();
    
    switch(param->evt_id)
    {
        case EVT_ID_SCRN_DELETE_REQ:
        case EVT_ID_SCRN_DELETE_REQ_IN_END_KEY:
        case EVT_ID_SCRN_GOBACK_IN_END_KEY:
            if (usb_state == MMI_USBSTATUS_MS_PROC)
            {
                return MMI_RET_ERR;
            }
            else
            {
                return MMI_RET_ALLOW_CLOSE;
            }

        case EVT_ID_SCRN_GOBACK:
            return MMI_RET_ALLOW_CLOSE;
			
        default:
            return MMI_RET_ALLOW_CLOSE;
    }
    return MMI_RET_ALLOW_CLOSE; 
}

/*****************************************************************************
 * FUNCTION
 *  mmi_usb_close_progress_screen
 * DESCRIPTION
 *  Close USB progress screen
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_close_progress_screen(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (GetActiveScreenId() == SCR_ID_USB_PROGRESS)
    {
        mmi_frm_scrn_close_active_id();
    }
    else
    {
        mmi_frm_scrn_close(GRP_ID_USB_MMI, SCR_ID_USB_PROGRESS);
    }
}

/*****************************************************************************
 * FUNCTION
 *  mmi_usb_is_in_mass_storage_mode
 * DESCRIPTION
 *  Get current USB mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
pBOOL mmi_usb_is_in_mass_storage_mode(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_USB_STATUS usb_state = GetUsbStatus();

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (usb_state == MMI_USBSTATUS_MS || usb_state == MMI_USBSTATUS_MS_PROC)
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_is_leaving_ms_mode
 * DESCRIPTION
 *  Get the USB status
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
pBOOL mmi_usb_is_leaving_ms_mode(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if ((MMI_USB_CTX(usb_status_prev) == MMI_USBSTATUS_MS && MMI_USB_CTX(usb_status) == MMI_USBSTATUS_MS_PROC) ||
        (!MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT) && MMI_USB_CTX(usb_status) == MMI_USBSTATUS_MS_PROC) ||
        (counter_of_reinit!=0))
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_app_deinit
 * DESCRIPTION
 *  App de-init for entering USB mode, ex: close file handler in use
 * PARAMETERS
 *  p       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_app_deinit(void *p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_APP_DEINIT);
    
    for (; counter_of_deinit < MAX_DEINIT_CALLBACK + 1; counter_of_deinit++)
    {
        U8 to_perform = TRUE;

        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_APP_DEINIT_COUNTER, counter_of_deinit);
        
        if (counter_of_deinit &&
            mmi_usb_deinit_callback[counter_of_deinit - 1].RspHdlr &&
            mmi_usb_deinit_callback[counter_of_deinit - 1].checkfunc &&
            mmi_usb_deinit_callback[counter_of_deinit - 1].checkfunc())
        {
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_APP_DEINIT_COUNTER_MINUS1_PROCEDURE);

            mmi_usb_deinit_callback[counter_of_deinit - 1].RspHdlr(p);
            SetProtocolEventHandler(
                mmi_usb_deinit_callback[counter_of_deinit - 1].RspHdlr,
                mmi_usb_deinit_callback[counter_of_deinit - 1].eventID);
        }

        if (mmi_usb_deinit_callback[counter_of_deinit].checkfunc)
        {
            to_perform = mmi_usb_deinit_callback[counter_of_deinit].checkfunc();
        }
        MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_APP_DEINIT_PERFORM, to_perform);
        
        if (to_perform)
        {
            if (mmi_usb_deinit_callback[counter_of_deinit].callback)
            {
                mmi_usb_deinit_callback[counter_of_deinit].callback();
            }
            else
            {
                MMI_ASSERT(counter_of_deinit == MAX_DEINIT_CALLBACK);
            }

            if (mmi_usb_deinit_callback[counter_of_deinit].eventID != 0)
            {
                MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_APP_DEINIT_EVENT_ID_RETURN);

                SetProtocolEventHandler(mmi_usb_app_deinit, mmi_usb_deinit_callback[counter_of_deinit].eventID);
                counter_of_deinit++;
                return;
            }
        }
    }

    /* reach here only when all callback are done */
    if (mmi_usb_deinit_callback[MAX_DEINIT_CALLBACK].callback == NULL)
    {
    #ifdef __MTP_ENABLE__
        if (mmi_usb_ptr->usb_ms_storage_owner == MMI_USB_MS_STORAGE_MTP)
        {
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_MTP);
        }
        else
    #endif
        {
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_MASS_STORAGE);
        }
    }

    counter_of_deinit = 0;
    mmi_usb_deinit_callback[MAX_DEINIT_CALLBACK].callback = NULL;
    
#if defined(__DM_LAWMO_SUPPORT__)
    if (g_mmi_usb_dmui_lock_process==MMI_TRUE)
    {
    	if (MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
        {
    	    SetUsbStatus(MMI_USBSTATUS_PLUGIN);
        }
        else
        {
            SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
        }
        mmi_usb_close_progress_screen();
        mmi_dmui_lawmo_report(MMI_DMUI_APP_ID_USB,
                              MMI_DMUI_CMD_LAWMO_PARTIALLYLOCK,
                              MMI_DMUI_RESULT_SUCCESS);
                              
        return;
    }
#endif    
    
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_app_reinit
 * DESCRIPTION
 *  App store for exiting from USB mode, ex: restore file handler in use
 * PARAMETERS
 *  p       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_app_reinit(void *p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (; counter_of_reinit < MAX_REINIT_CALLBACK; counter_of_reinit++)
    {
        U8 to_perform = TRUE;

        if (counter_of_reinit &&
            mmi_usb_reinit_callback[counter_of_reinit - 1].RspHdlr &&
            mmi_usb_reinit_callback[counter_of_reinit - 1].checkfunc &&
            mmi_usb_reinit_callback[counter_of_reinit - 1].checkfunc())
        {
            mmi_usb_reinit_callback[counter_of_reinit - 1].RspHdlr(p);
            SetProtocolEventHandler(
                mmi_usb_reinit_callback[counter_of_reinit - 1].RspHdlr,
                mmi_usb_reinit_callback[counter_of_reinit - 1].eventID);
        }

        if (mmi_usb_reinit_callback[counter_of_reinit].checkfunc)
        {
            to_perform = mmi_usb_reinit_callback[counter_of_reinit].checkfunc();
        }

        if (to_perform)
        {
            if (mmi_usb_reinit_callback[counter_of_reinit].callback)
            {
                mmi_usb_reinit_callback[counter_of_reinit].callback();
            }
            else
            {
                MMI_ASSERT(0);  /* break; */
            }

            if (mmi_usb_reinit_callback[counter_of_reinit].eventID != 0)
            {
                SetProtocolEventHandler(mmi_usb_app_reinit, mmi_usb_reinit_callback[counter_of_reinit].eventID);
                counter_of_reinit++;
                return;
            }
        }
    }

    /* reach here only when all callback are done */
    counter_of_reinit = 0;
    mmi_usb_send_removal_event_rsp(NULL);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_check_path_exported
 * DESCRIPTION
 *  To check if the path is exported to PC
 * PARAMETERS
 *  path        [?]     
 * RETURNS
 *  void
 *****************************************************************************/
pBOOL mmi_usb_check_path_exported(S8 *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
#ifndef __USB_IN_NORMAL_MODE_IMPROVE__	    
    U16 unicode_buf[10] = {0};
#endif    

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __USB_IN_NORMAL_MODE_IMPROVE__	

    if (mmi_ucs2ncmp(path, "Z:\\", 3)==0)
    {
    	return MMI_FALSE;
    }
    else
    {
    	return MMI_TRUE;
    }
    
#else    

    mmi_asc_n_to_ucs2((S8*) unicode_buf, path, 3);

    if (FS_GetDevType((PU16) unicode_buf) != FS_GetDevType(L"Z:\\"))
    {
        return MMI_TRUE;
    }
    else
    {
        return MMI_FALSE;
    }
#endif /* __USB_IN_NORMAL_MODE_IMPROVE__	 */    

}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_app_unavailable_popup
 * DESCRIPTION
 *  To make a warning if the app. is disable in MS mode
 * PARAMETERS
 *  stringId        [IN]        0 will popup "USB: Can't access the storage now"; app specific string otherwise
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_usb_app_unavailable_popup(U16 stringId)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_usb_enter_normal_group();
    if (stringId == 0)
    {
        mmi_popup_display_simple((WCHAR*)GetString(STR_ID_USB_MODE_EXPORTED_WARNING), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
    }
    else
    {
        mmi_popup_display_simple((WCHAR*)GetString(stringId), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
    }

}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_get_error_info
 * DESCRIPTION
 *  Function to get the error information includes popup type and string
 * PARAMETERS
 *  error_result:   [IN]    error result type
 *  popup_type:     [OUT]   get the error popup type
 * RETURNS
 *  Get the error popup string id.
 *****************************************************************************/
MMI_STR_ID mmi_usb_get_error_info(S32 error_result, mmi_event_notify_enum* popup_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_STR_ID  str_id = STR_ID_USB_MODE_EXPORTED_WARNING;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    *popup_type = MMI_EVENT_FAILURE;
    if (error_result == MMI_USB_ERR_IS_UNAVAILABLE)
    {
        str_id = STR_ID_USB_MODE_EXPORTED_WARNING;
    }
    return str_id;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_send_removal_event_rsp
 * DESCRIPTION
 *  removal response handler
 * PARAMETERS
 *  info        [?]     response message
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_send_removal_event_rsp(void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__DM_LAWMO_SUPPORT__)
    if (g_mmi_usb_dmui_lock_process==MMI_TRUE)
    {
    	if (MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
        {
    	    SetUsbStatus(MMI_USBSTATUS_PLUGIN);
        }
        else
        {
            SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
        }
        mmi_usb_close_progress_screen();
        mmi_dmui_lawmo_report(MMI_DMUI_APP_ID_USB,
                              MMI_DMUI_CMD_LAWMO_PARTIALLYLOCK,
                              MMI_DMUI_RESULT_SUCCESS);
                              
        return;
    }
#endif    
    
    if (mmi_usb_ptr->ms_config_result == MMI_FALSE)
    {
        mmi_usb_ptr->ms_config_result = MMI_TRUE;

        if (MMIUSB_GET_FLAG(MMIUSB_MASK_IS_CONNECT))
        {
            SetUsbStatus(MMI_USBSTATUS_PLUGIN);
            mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_UNFINISHED), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
        }
        else
        {
            SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
            BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
        }
        mmi_usb_close_progress_screen();
        mmi_usb_close_detect_query_list();
    }
    else
    //if (PermitUsbCfgScreenDisplay() == MMI_TRUE)
    {
        //if (GetActiveScreenId() != POPUP_SCREENID)
        {
            BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
        }
        SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
        mmi_usb_close_progress_screen();
    }
}


#ifdef MMS_SUPPORT
/*****************************************************************************
 * FUNCTION
 *  mmi_usb_mms_check_to_deinit
 * DESCRIPTION
 *  decide to perform callback or not
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static U8 mmi_usb_mms_check_to_deinit(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (wap_is_ready())
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_mms_check_to_reinit
 * DESCRIPTION
 *  removal response handler
 * PARAMETERS
 *  void
 *  info: response message(?)
 * RETURNS
 *  void
 *****************************************************************************/
static U8 mmi_usb_mms_check_to_reinit(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (wap_is_ready())
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
#endif /* MMS_SUPPORT */


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_is_any_exported_drive
 * DESCRIPTION
 *  is any exported drive
 * PARAMETERS
 *  void
 * RETURNS
 *  True or False
 *****************************************************************************/
MMI_BOOL mmi_usb_is_any_exported_drive(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

#ifdef __USB_IN_NORMAL_MODE_IMPROVE__	

    return MMI_TRUE;
    
#elif (defined(__FS_CARD_SUPPORT__) && !defined(NAND_SUPPORT)) || (defined(__FS_CARD_SUPPORT__) && defined(_NAND_FLASH_BOOTING_))
    if (!srv_fmgr_drv_has_accessible_removable_drv())
    {
        return MMI_FALSE;
    }
    else
#endif /* #ifdef __USB_IN_NORMAL_MODE_IMPROVE__ */
    {
        return MMI_TRUE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_shutdown_handler
 * DESCRIPTION
 *  Hide file related menus before enter USB mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
mmi_ret mmi_usb_shutdown_handler(mmi_event_struct *evt)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (GetUsbStatus() == MMI_USBSTATUS_MS)
    {
       if (mmi_usb_ptr->usb_ms_storage_owner == MMI_USB_MS_STORAGE_MTP)
       {
       #ifdef __MTP_ENABLE__
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MTP);
	#endif
        }
        else
    	  {
            SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MS);
        }
        SetUsbStatus(MMI_USBSTATUS_MS_PROC);
    }

    return MMI_RET_OK;
}

#endif /* __USB_IN_NORMAL_MODE__ */ 


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_send_removal_event_req
 * DESCRIPTION
 *  removal request handler
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_send_removal_event_req(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_SEND_REMOVAL_EVENT_REQ);

    if (srv_bootup_get_booting_mode() == SRV_BOOTUP_MODE_USB)
    {
        if (!mmi_alm_is_in_alarm())
        {
            SetInterruptPopupDisplay(POPUP_ALL_OFF);
            UsbSendMsgToHW(PRT_POWER_OFF_REQ, (void*)NULL, (void*)NULL);
        }
        else
        {
            SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
            BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
            mmi_frm_scrn_close(mmi_usb_get_boot_group(), SCR_USB_POWER_ON);
            mmi_alm_enable_pwroff();
        }
    }
    else
    {
    #ifdef __USB_IN_NORMAL_MODE__
        MMI_USB_STATUS usb_state = GetUsbStatus();

        if ((usb_state == MMI_USBSTATUS_MS) || (usb_state == MMI_USBSTATUS_IGNORED))
        {
            if (!mmi_usb_permit_usb_cfg_screen_display_test() ||
                PermitUsbCfgScreenDisplay() == MMI_FALSE)
            {
        //        BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
            }
            else
            {
                mmi_usb_entry_progress_screen();
            }

            SetUsbStatus(MMI_USBSTATUS_MS_PROC);
        }
        else if (usb_state == MMI_USBSTATUS_MS_PROC)
        {
            if (!mmi_usb_permit_usb_cfg_screen_display_test() ||
                PermitUsbCfgScreenDisplay() == MMI_TRUE)
            {
                //BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
            }

            if (counter_of_reinit)
            {
                return; /* reinit already proceeding */
            }
            else if (counter_of_deinit &&
            	((!mmi_usb_deinit_callback[MAX_DEINIT_CALLBACK].callback) 
        #if defined(__DM_LAWMO_SUPPORT__)
            	|| g_mmi_usb_dmui_lock_process==MMI_TRUE
        #endif
            	))
            {
                /* '==' means come twice */
                mmi_usb_deinit_callback[MAX_DEINIT_CALLBACK].callback = mmi_usb_send_removal_event_req;
                return;
            }
            else if (counter_of_deinit && (counter_of_deinit < MAX_DEINIT_CALLBACK-1))
            {
            	return;
            }
        }

        mmi_usb_close_detect_query_list();

        mmi_usb_restore_file_related_menus();
        mmi_usb_app_reinit(NULL);
    #else
        if (srv_bootup_is_booting() == MMI_FALSE)
        {
            BatteryIndicationPopup(STR_USB_CHARGER_REMOVED);
        }
        SetUsbStatus(MMI_USBSTATUS_PLUGOUT);
    #endif /* __USB_IN_NORMAL_MODE__ */ 
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_send_power_on_req
 * DESCRIPTION
 *  Send power on request (END key is long pressed) to L4C in USB power on mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void mmi_usb_send_power_on_req(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_eq_power_on_req_struct *myMsgPtr;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    myMsgPtr = (mmi_eq_power_on_req_struct*) OslConstructDataPtr(sizeof(mmi_eq_power_on_req_struct));
    myMsgPtr->fun = mmi_usb_ptr->reset_type;
    myMsgPtr->rst = 0;
    mmi_frm_send_ilm((oslModuleType) MOD_L4C, MSG_ID_MMI_EQ_POWER_ON_REQ, 
                        (oslParaType*)myMsgPtr, (oslPeerBuffType*)NULL);
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_set_reset_type
 * DESCRIPTION
 *  Set reset type
 * PARAMETERS
 *  reset_type      [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_usb_set_reset_type(U8 reset_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mmi_usb_ptr->reset_type = reset_type;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_set_background_still
 * DESCRIPTION
 *  Set background image
 * PARAMETERS
 *  isStill     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_usb_set_background_still(MMI_BOOL isStill)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (isStill)
    {
        mmi_usb_ptr->still_image = MMI_USB_POWERON_CHARGING_DISABLE;
    }
    else
    {
        mmi_usb_ptr->still_image = MMI_USB_POWERON_CHARGING_ENABLE;
    }

    if (mmi_frm_scrn_get_active_id() == SCR_USB_POWER_ON)
    {
        ShowUsbScr();
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_usb_set_background_still
 * DESCRIPTION
 *  Set background image
 * PARAMETERS
 *  isStill     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
BOOL mmi_usb_is_network_services_available(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_SINGLE_CALL__
    E_MTPNP_AD_SIMCARD_USABLE_SIDE state;
#endif

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_DUAL_SIM_SINGLE_CALL__
    if (srv_nw_usab_is_usable(MMI_SIM1) ||
        srv_nw_usab_is_usable(MMI_SIM2))
    {
        return TRUE;
    }
#ifdef __MMI_WLAN_FEATURES__
    if (mmi_netset_get_active_preferred_mode() > 0)
    {
        return TRUE;
    }
#endif 
    return FALSE;

#else /* __MMI_DUAL_SIM_SINGLE_CALL__ */

    return mmi_bootup_is_network_service_available();

#endif /* __MMI_DUAL_SIM_SINGLE_CALL__ */
}


#if defined(__DM_LAWMO_SUPPORT__)
/*****************************************************************************
 * FUNCTION
 *  mmi_usb_set_background_still
 * DESCRIPTION
 *  Set background image
 * PARAMETERS
 *  isStill     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_usb_dmui_lock_notify(mmi_dmui_app_id_enum app_id, mmi_dmui_cmd_enum cmd)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/	
#if defined(__USB_IN_NORMAL_MODE__)
    if (cmd == MMI_DMUI_CMD_LAWMO_PARTIALLYLOCK)
    {
        /*operate lock*/
        g_mmi_usb_dmui_lock_process = MMI_TRUE;    
        	
        if (GetUsbStatus()== MMI_USBSTATUS_MS)
        {
        	if (mmi_usb_ptr->usb_ms_storage_owner == MMI_USB_MS_STORAGE_MTP)
            {
               #ifdef __MTP_ENABLE__
                    SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MTP);
               #endif
            }
            else
            {
                SendUsbConfigToHW(DEVUSB_CFG_ACTION_STOP_MS);
            }
        }
        else if (GetUsbStatus()== MMI_USBSTATUS_MS_PROC)
        {
            /* Wait deinit or reinit finished to report back to DM*/
            return;
        }
        else
        {
            mmi_dmui_lawmo_report(MMI_DMUI_APP_ID_USB,
                                  MMI_DMUI_CMD_LAWMO_PARTIALLYLOCK,
                                  MMI_DMUI_RESULT_SUCCESS); 
               
        }
    }
    else if (cmd == MMI_DMUI_CMD_LAWMO_UNLOCK)
    {
       /*operate unlock*/        
       mmi_dmui_lawmo_report( MMI_DMUI_APP_ID_USB,
                              MMI_DMUI_CMD_LAWMO_UNLOCK,
                              MMI_DMUI_RESULT_SUCCESS);

       g_mmi_usb_dmui_lock_process = MMI_FALSE;     
    }
#endif    /* __USB_IN_NORMAL_MODE__ */
}
#endif

/* Screen group*/
static MMI_ID g_usb_boot_mode_group = GRP_ID_INVALID;
static MMI_ID g_usb_normal_mode_group = GRP_ID_INVALID;
#ifdef MMI_USB_MENU_CUI
static MMI_ID g_query_list_menu_id = GRP_ID_INVALID;
#endif

static void mmi_usb_enter_usb_boot_group(void)
{
    MMI_ID gid;

    if (g_usb_boot_mode_group != GRP_ID_INVALID)
    {
        return;
    }
    gid = mmi_frm_group_create(GRP_ID_ROOT, GRP_ID_USB_BOOT_MODE, mmi_usb_boot_group_proc, NULL);
    mmi_frm_group_enter(gid, 0);

    g_usb_boot_mode_group = gid;
}

static void mmi_usb_close_usb_boot_group(void)
{
    if (g_usb_boot_mode_group == GRP_ID_INVALID)
    {
        //kal_prompt_trace(MOD_MMI, "[USB MMI] GRP_ID_INVALID");
        return;
    }
    mmi_frm_group_close(g_usb_boot_mode_group);
}

static mmi_ret mmi_usb_boot_group_proc(mmi_event_struct *param)
{
    switch(param->evt_id)
    {
        case EVT_ID_GROUP_DEINIT:
            g_usb_boot_mode_group = GRP_ID_INVALID;
            break;

        default:
            break;
    }
    return MMI_RET_OK;
}

static MMI_ID mmi_usb_get_boot_group(void)
{
    mmi_usb_enter_usb_boot_group();
    return g_usb_boot_mode_group;
}


static void mmi_usb_enter_normal_group(void)
{
    MMI_ID gid;

    if (g_usb_normal_mode_group != GRP_ID_INVALID)
    {
        return;
    }
    gid = mmi_frm_group_create(GRP_ID_ROOT, GRP_ID_USB_MMI, mmi_usb_normal_group_proc, NULL);
    mmi_frm_group_enter(gid, MMI_FRM_NODE_SMART_CLOSE_FLAG);

    g_usb_normal_mode_group = gid;
}

static MMI_ID mmi_usb_get_normal_group(void)
{
    mmi_usb_enter_normal_group();
    return g_usb_normal_mode_group;
}

static mmi_ret mmi_usb_normal_group_proc(mmi_event_struct *param)
{
#ifdef MMI_USB_MENU_CUI
    cui_menu_event_struct *menu_evt;

    menu_evt = (cui_menu_event_struct*)param;
#endif

    //kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_normal_group_proc");

#ifdef MMI_USB_MENU_CUI    
    switch(menu_evt->evt_id)
#else
    switch(param->evt_id)
#endif
    {
        case EVT_ID_GROUP_DEINIT:
            //kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_GROUP_DEINIT");
            g_usb_normal_mode_group = GRP_ID_INVALID;
            break;
            
#ifdef MMI_USB_MENU_CUI

        case EVT_ID_CUI_MENU_LIST_ENTRY:
            //kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_CUI_MENU_LIST_ENTRY");
            mmi_usb_init_query_list(menu_evt->sender_id);
            break;

        case EVT_ID_CUI_MENU_ITEM_SELECT:
            //kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_CUI_MENU_ITEM_SELECT");
            mmi_usb_query_list_select_hdlr(menu_evt);
            break;

        case EVT_ID_CUI_MENU_LIST_EXIT:
            //kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_CUI_MENU_LIST_EXIT");
            //SetUsbStatus(MMI_USBSTATUS_IGNORED);
            break;
            
        case EVT_ID_CUI_MENU_CLOSE_REQUEST:
            //kal_prompt_trace(MOD_MMI, "[USBMMI] EVT_ID_CUI_MENU_CLOSE_REQUEST");
            SetUsbStatus(MMI_USBSTATUS_IGNORED);
            cui_menu_close(menu_evt->sender_id);
            g_query_list_menu_id = GRP_ID_INVALID;
            break;

#endif
        default:
            break;
    }

    return MMI_RET_OK;
}

#if 1
typedef enum
{
#ifdef __USB_MASS_STORAGE_ENABLE__
    MMI_USB_QUERY_LIST_MS = 0,
#endif
#ifdef __MMI_WEBCAM__
    MMI_USB_QUERY_LIST_WEBCAM,
#endif
#ifdef __USB_COM_PORT_ENABLE__
    MMI_USB_QUERY_LIST_CDCAM_DATA,
#endif
#ifdef __MMI_PICT_BRIDGE_SUPPORT__
    MMI_USB_QUERY_LIST_PICT_BRIGE,
#endif
#if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
    MMI_USB_QUERY_LIST_COMPORT_JAVA,
#endif
#ifdef  __MTP_ENABLE__
    MMI_USB_QUERY_LIST_MTP,
#endif
    MMI_USB_QUERY_LIST_COUNT
} mmi_usb_query_list_id_enum;

static const MMI_MENU_ID g_query_list_id[] = 
{
#ifdef __USB_MASS_STORAGE_ENABLE__
    MMI_USB_QUERY_LIST_MS,
#endif
#ifdef __MMI_WEBCAM__
    MMI_USB_QUERY_LIST_WEBCAM,
#endif
#ifdef __USB_COM_PORT_ENABLE__
    MMI_USB_QUERY_LIST_CDCAM_DATA,
#endif
#ifdef __MMI_PICT_BRIDGE_SUPPORT__
    MMI_USB_QUERY_LIST_PICT_BRIGE,
#endif
#if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
    MMI_USB_QUERY_LIST_COMPORT_JAVA,
#endif
#ifdef  __MTP_ENABLE__
    MMI_USB_QUERY_LIST_MTP
#endif
};

static const U16 g_query_list_str[] = 
{
#ifdef __USB_MASS_STORAGE_ENABLE__        
    STR_USB_CONFIG_MS,
#endif
#ifdef __MMI_WEBCAM__
    STR_ID_USB_CONFIG_WEBCAM,
#endif 
#ifdef __USB_COM_PORT_ENABLE__
    STR_USB_CONFIG_CDCACM_DATA,
#endif
#ifdef __MMI_PICT_BRIDGE_SUPPORT__
    STR_USB_CONFIG_PICT_BRIGE,
#endif
#if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
    STR_USB_CONFIG_COMPORT_JAVA,
#endif
#ifdef  __MTP_ENABLE__
    STR_USB_CONFIG_MTP
#endif
};

/* Menu CUI*/
#ifdef MMI_USB_MENU_CUI

static void mmi_usb_enter_detect_query_list(void)
{
    mmi_usb_enter_normal_group();
    g_query_list_menu_id = cui_menu_create(GRP_ID_USB_MMI, 
        CUI_MENU_SRC_TYPE_APPCREATE, 
        CUI_MENU_TYPE_APPSUB, 
        0, 
        MMI_FALSE, 
        NULL);

    cui_menu_run(g_query_list_menu_id);
}

static void mmi_usb_init_query_list(MMI_ID menu_id)
{
    S32 i;
    
    cui_menu_set_currlist_title(menu_id, (WCHAR*)GetString(STR_USB_CONFIG_TITLE), NULL);
    cui_menu_set_currlist(menu_id, MMI_USB_QUERY_LIST_COUNT, (MMI_MENU_ID*)g_query_list_menu_id);
    cui_menu_set_currlist_left_softkey(menu_id, (WCHAR*)GetString(STR_GLOBAL_OK));
    cui_menu_set_currlist_right_softkey(menu_id, (WCHAR*)GetString(STR_GLOBAL_BACK));
    
    for (i = 0; i < MMI_USB_QUERY_LIST_COUNT; i++)
    {
        cui_menu_set_item_string(menu_id, i, (WCHAR*)GetString(g_query_list_str[i]));
    }
}

static void mmi_usb_query_list_select_hdlr(cui_menu_event_struct *event)
{
    //kal_prompt_trace(MOD_MMI, "[USBMMI] mmi_usb_query_list_select_hdlr");
    
#if defined(__DM_LAWMO_SUPPORT__)
    if (g_mmi_usb_dmui_lock_process == MMI_TRUE)
    {
        //kal_prompt_trace(MOD_MMI, "[USBMMI] lock");
        mmi_popup_display_simple((WCHAR*)GetString(STR_GLOBAL_INVALID), MMI_EVENT_FAILURE, mmi_usb_get_normal_group(), NULL);
        return;
    }
#endif

#ifdef __MMI_DSM_NEW__
	if(MR_SUCCESS == mr_app_system_event_hdlr(MR_SYSMSG_ENTER_USB, 0, 0))
		return;
#endif
    //kal_prompt_trace(MOD_MMI, "[USBMMI] menu_id=%d", event->highlighted_menu_id);

    switch(event->highlighted_menu_id)
    {
    #ifdef __USB_MASS_STORAGE_ENABLE__
        case MMI_USB_QUERY_LIST_MS:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_MS);
            mmi_usb_ptr->usb_ms_storage_owner = MMI_USB_MS_STORAGE_MS;
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_MASS_STORAGE);
            break;
    #endif

    #ifdef __MMI_WEBCAM__
        case MMI_USB_QUERY_LIST_WEBCAM:
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_WEBCAM);
            break;
    #endif
    
    #ifdef __USB_COM_PORT_ENABLE__
        case MMI_USB_QUERY_LIST_CDCAM_DATA:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_CDCACM);
            mmi_usb_ptr->usb_comport_owner = MMI_USB_COMPORT_PS;			
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_CDC_ACM);
            break;
    #endif
    
    #ifdef __MMI_PICT_BRIDGE_SUPPORT__
        case MMI_USB_QUERY_LIST_PICT_BRIGE:
            #ifdef MMI_ON_HARDWARE_P
                UsbConfigFirstPage(DEVUSB_CFG_ACTION_PICTBRIGE);
            #else /* MMI_ON_HARDWARE_P */ 
                mmi_frm_scrn_close_active_id();
            #endif /* MMI_ON_HARDWARE_P */   
            break;
    #endif
    
    #if defined(__J2ME__) && defined(__USB_COM_PORT_ENABLE__)
        case MMI_USB_QUERY_LIST_COMPORT_JAVA:
            mmi_usb_ptr->usb_comport_owner = MMI_USB_COMPORT_JAVA;
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_CDC_ACM);
            break;
    #endif
    
    #ifdef  __MTP_ENABLE__
        case MMI_USB_QUERY_LIST_MTP:
            mmi_usb_ptr->usb_ms_storage_owner = MMI_USB_MS_STORAGE_MTP;
            UsbConfigFirstPage(DEVUSB_CFG_ACTION_MTP);
            break;
    #endif

        default:
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_USB_CONF_AS_NOTHING);
            mmi_frm_scrn_close_active_id();
            break;
    }
    cui_menu_close(event->sender_id);
    g_query_list_menu_id = GRP_ID_INVALID;
}

#endif /* MMI_USB_MENU_CUI*/

static void mmi_usb_close_detect_query_list(void)
{
#ifdef MMI_USB_MENU_CUI
    if (g_query_list_menu_id != GRP_ID_INVALID)
    {
        cui_menu_close(g_query_list_menu_id);
        g_query_list_menu_id = GRP_ID_INVALID;
    }
#endif
    mmi_frm_scrn_close(GRP_ID_USB_MMI, SCR_DEV_USBDETECT);
}

static mmi_ret mmi_usb_detect_cfg_leave_proc(mmi_event_struct *param)
{
    MMI_USB_STATUS usb_state = GetUsbStatus();
    
    switch(param->evt_id)
    {
        case EVT_ID_SCRN_GOBACK_IN_END_KEY:
            SetUsbStatus(MMI_USBSTATUS_IGNORED);
            break;

        default:
            break;
    }
    return MMI_RET_ALLOW_CLOSE; 
}


#endif
#endif /* __MMI_USB_SPLIT_SUPPORT__*/
#else /* __MMI_USB_SUPPORT__ */

#include "MMIDataType.h"

/*****************************************************************************
 * FUNCTION
 *  mmi_usb_shutdown_handler
 * DESCRIPTION
 *  Hide file related menus before enter USB mode
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
mmi_ret mmi_usb_shutdown_handler(mmi_event_struct *evt)
{
    return MMI_RET_OK;
}

mmi_ret mmi_usb_handle_query_permit_show(mmi_event_struct *evt)
{
    return MMI_RET_OK;
}

mmi_ret mmi_usb_handle_query_permit_pre_check(mmi_event_struct *evt)
{
    return MMI_RET_OK;
}

mmi_ret mmi_usb_handle_plug_in(mmi_event_struct *evt)
{
	  return MMI_RET_OK;
}

mmi_ret mmi_usb_config_rsp_callback(mmi_event_struct *evt)
{
    return MMI_RET_OK;
}

mmi_ret mmi_usb_mmi_notify_callback(mmi_event_struct *evt)
{
    return MMI_RET_OK;
}

#endif /* __MMI_USB_SUPPORT__ */ 

